iPad SplitView changes main navigation bar color

2019-04-10 05:51发布

问题:

Weird problem: After rotating my app to portrait, picking the toolbar item and exposing the uipopovercontroller, if I rotate back to landscape, the UINavigationController on the right side (objectAtIndex:0 of the SplitView) changes the color of the navigation bar. I am not sure why. I have it set in Interface Builder to be barStyle = UIBarStyleBlackOpaque;

It turns silver after it returns to landscape mode.

This only happens if I rotate it to portrait, create the popover, and select something in the navigation controller, which pushes another tableViewController. Even setting the properties in the viewDidLoad method does nothing.

Anyone have an idea?

回答1:

viewDidLoad will only get called the first time your view is displayed (or if it's cleared due to memory issues). Try re-setting the barStyle in your viewWillAppear, or even – splitViewController:willShowViewController:invalidatingBarButtonItem:.



回答2:

for Steve (detect PoPView or splitView navigationBar of RootViewControll) [splitview IPAD]

Yuo have to create a class method (setLand:int i) on RootViewController called from detailviewcontroller in these method:

- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    // LANDSCAPE !!!!
    [RootViewController setLand:1];

and

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
    // PORTRAIT!!
    [RootViewController setLand:0];

and on RootViewController:

    static int landscape=2;

    ...

// SetMethod for class variable landscape   
        + (void)setLand:(int)i 
        {
            if(landscape!=i){
                landscape = i;

            }
        }

and finaly alway in RootViewController

 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     if(landscape == 1)
     {
       //LANDSCAPE!
       CUSTOM FOR LANDSCAPE
       self.navigationController.navigationBar.barStyle =...
     }
     else if(landscape == 0)
     {
       //PORTRAIT!
       CUSTOM FOR PORTRAIT
       self.navigationController.navigationBar.barStyle =...
     }
   }

..this works well in my app, editing custom landscape/portrait navigationBar



回答3:

There seems to be an issue with 4.2 and setting the tintColor of the navigationBar after rotation. You can set the barStyle correctly using the answers above, but not the tintColor. Anybody else having the same issue?



回答4:

@Brendan G. Lim and any others having trouble with the tintColor, finally got it working with a custom navigation bar:

  1. Create a custom navigation file subclassed from UINavigationBar
@interface CustomNavigationBar : UINavigationBar 
{
}

@end
  1. In your implementation file, override the setTintColor method
@implementation CustomNavigationBar

-(void)setTintColor:(UIColor *)tintColor
{
  [super setTintColor :[self tintColor]];
}

@end
  1. Open MainWindow.xib, and select your navigation bar you want to set the color to. In your Identity pane [Apple][4] select CustomNavigationBar as the class.

  2. In the attributes pane [Apple][1] set the color of the bar.

That's it!



回答5:

I'm having this same problem but resetting the barStyle in viewWillAppear causes another problem. Setting it there also sets it when it is shown in the popover, so it no longer matches the popover color. How can I set the barStyle to what I want only when it is being shown in the left pane of the split view controller? I guess I could set it in view will appear only when the orientation is landscape but that seems dirty. Also, setting it in splitViewController:willShowViewController:invalidatingBarButtonItem: does not work at all since I think this is called before the split view controller sets the styles back to default. This seems like a really stupid bug on apple's part. It should be changing it back to the style it originally was, not the default one.



回答6:

Filed a bug report about this weeks ago and Apple said that this is a known bug of 4.2. I then asked if there was a way to fix this, but no reply so far.

It's also no use replacing the UINavigationbar with a subclassed/customized navigation bar. The popover seems to perform some secret nasty stuff on the UINavigatioBar which kills the tintColor and won't allow to reset it (it'll always remain 'nil', even after resetting it).

I basically gave up and told the customer he'll have to live with it until the next update is out (hopefully).



回答7:

This problem is fixed used the following code

@implementation ChangeNavigationBarColor

- (void) setTintColor:(UIColor*)color
{
    [super setTintColor:[[BrandingManager sharedBrandingManager] tintColorForNavigationController]];
}


@interface ChangeNavigationBarColor : UINavigationBar {

}

@end


回答8:

Cool, fixed it.

Added to my RootViewController where the splitviewcontroller and nav bar is declared:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];


    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

}


回答9:

You can use a separate class for changing color, make the background color that class , your desired color, and then use that class as class of your rootViewController. I did, it works.