iOS 7 : Disable UINavigationBar Translucency For Entire App

后端 未结 9 1846
Happy的楠姐
Happy的楠姐 2020-12-14 14:12

Is there a way to disable UINavigationBar Translucency for an entire application?

I\'m aware that using [self.navigationController.navigationBar setTranslucent

相关标签:
9条回答
  • 2020-12-14 14:21

    It seems very simple with this code in appDelegate didFinishLaunchingWithOptions (works fine with iOS 8 and above versions)

    [[UINavigationBar appearance] setTranslucent:NO];
    
    0 讨论(0)
  • 2020-12-14 14:24

    if you set the translucence of the first navigation bar in the stack to false [self.navigationController.navigationBar setTranslucent:NO], it will reflect in all the following NavigationViewController that are pushed to that stack.

    0 讨论(0)
  • 2020-12-14 14:28

    I think appearance api does not support translucent property of navigation bar . But you can do this for whole App like this , please have a look at this code --

    here Menu Screen is a root view controller .

    MenuScreen *ms = [[MenuScreen alloc]initWithNibName:@"MenuScreen" bundle:nil];
    
    UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:ms];
    
    //This will set property for whole App.
    [nv.navigationBar setTranslucent:NO];
    
    self.window.rootViewController = nv ;
    
    0 讨论(0)
  • 2020-12-14 14:31

    I know this is old, but this might come in handy for someone;

    You can use a category, and within it* set the property [translucent][1]

    @implementation UINavigationBar (MakeTranslucent)
    
    -(void)willMoveToWindow:(UIWindow *)newWindow {
        [super willMoveToWindow:newWindow];
    
    
        self.translucent = NO;
    }
    @end
    
    • I used willMoveToWindow, I do not know whether this is a good idea so UAYOR.
    0 讨论(0)
  • 2020-12-14 14:36

    I think you are right about no appearance proxy being available for this property. Are you using UINavigationControllers or UINavigationBar objects? If you are using UINavigationBars you could subclass it and create a non-translucent nav bar.

    Header file:

    #import <UIKit/UIKit.h>
    
    @interface ABCNonTranslucentNavBar : UINavigationBar
    
    @end
    

    Implementation file:

    #import "ABCNonTranslucentNavBar.h"
    
    @implementation ABCNonTranslucentNavBar
    
    - (void)drawRect:(CGRect)rect
    {
      [self setTranslucent:NO];
    }
    

    Then just replace the UINavigationBars with your subclass. You could also do something similar with a subclassed UINavigationController.

    0 讨论(0)
  • 2020-12-14 14:39

    See the excerpt from UIKit code documentation:

    /*
         New behavior on iOS 7.
         Default is YES.
         You may force an opaque background by setting the property to NO.
         If the navigation bar has a custom background image, the default is inferred 
         from the alpha values of the image—YES if it has any pixel with alpha < 1.0
         If you send setTranslucent:YES to a bar with an opaque custom background image
         it will apply a system opacity less than 1.0 to the image.
         If you send setTranslucent:NO to a bar with a translucent custom background image
         it will provide an opaque background for the image using the bar's barTintColor if defined, or black
         for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
         */
    

    Correct Swift 4 solution is

    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().backgroundColor = .white
    
    0 讨论(0)
提交回复
热议问题