Get the right color in iOS7 translucent navigation bar

后端 未结 20 1056
囚心锁ツ
囚心锁ツ 2020-11-28 00:59

How can I get the right coloring for my translucent navigation bars in iOS 7? The navigation bar just adjusts the given color to a much brighter one. Changing brightness or

相关标签:
20条回答
  • 2020-11-28 01:32

    You can run the application in the simulator and take the color of the navigation bar using the Eyedropper tool.

    0 讨论(0)
  • 2020-11-28 01:32

    Try rendering corresponding background image for your navigation bar. That worked for my test application.

    UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor colorWithRed:55.0f/256.0f green:0.0f blue:1.0f alpha:0.5f] set];
    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
    
    UIImage *navBarBackgroundImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsLandscapePhone];
    
    0 讨论(0)
  • 2020-11-28 01:33

    I suppose you have read all the comments above. If you want to get the custom background & translucency you should override the navigationbar class and implement your own layoutsubviews method. Simple add additional subview here. IMPORTANT: you should add it just above the background subview of the NavigationBar. It will hide your header or buttons if you just put it above the all subviews.

    Also, check out this question

    0 讨论(0)
  • 2020-11-28 01:34

    I've extented UINavigationController,
    on its viewDidLoad, i've added a background with the same color of the tint.

    Also, i've set the background frame to cover the status bar area:

        self.navigationBar.barTintColor = navBackgroundColor;
        CGRect bgFrame = self.navigationBar.bounds;
        bgFrame.origin.y -= 20.0;
        bgFrame.size.height += 20.0;
        UIView *backgroundView = [[UIView alloc] initWithFrame:bgFrame];
        backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        backgroundView.backgroundColor = navBackgroundColor;
        backgroundView.alpha = 0.6;
        [self.navigationBar addSubview:backgroundView];
        [self.navigationBar sendSubviewToBack:backgroundView];
    

    On my case alpha 0.6 got the job done, but you can play with it.

    0 讨论(0)
  • 2020-11-28 01:34

    If you're using swift 2.0 you can use this, this will remove the blur and have the color show properly.

    UINavigationBar.appearance().translucent = false
    
    0 讨论(0)
  • 2020-11-28 01:35

    This works for me:

    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, -20,navigationBar.frame.size.width,navigationBar.frame.size.height + 20);
    layer.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.75].CGColor;
    layer.zPosition = -5;
    [navigationBar.layer addSublayer:layer];
    
    0 讨论(0)
提交回复
热议问题