Change the alpha value of the navigation bar

后端 未结 9 1683
误落风尘
误落风尘 2020-12-08 05:55

Is this possible?

I want to change the alpha value of the navigation bar in my view controller (in an animation), but if I do self.navigationController.navigat

相关标签:
9条回答
  • 2020-12-08 05:58

    The most straightforward way of doing this is modifying the alpha component of navigationBar background view, which at this time (iOS9) is a first navigationBar subview. Note however that we never know if the subview hierarchy will be changed by apple in later releases, so gotta be careful.

    let navigationBackgroundView = self.navigationController?.navigationBar.subviews.first
    navigationBackgroundView?.alpha = 0.7
    
    0 讨论(0)
  • 2020-12-08 05:59

    You can also try to set a background view underneath the navigation bar and modify this view directly.

    private lazy var backgroundView: UIView = UIView()
    
    ...
    
    private func setupNavigationBarBackground() {
        guard let navigationBar = navigationController?.navigationBar else { return }
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.isTranslucent = true
        view.addSubview(backgroundView)
        backgroundView.alpha = 0
        backgroundView.backgroundColor = .red
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        backgroundView.topAnchor.constraint(equalTo: topLayoutGuide.topAnchor).isActive = true
        backgroundView.bottomAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
        backgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        backgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    }
    
    private func changeNavigationBarAlpha(to alpha: CGFloat) {
        backgroundView.alpha = alpha
    }
    
    0 讨论(0)
  • 2020-12-08 06:02

    MickBraun's answer in Swift:

    1. In AppDelegate.swift add these lines in didFinishLaunchingWithOptions:

      // create background images for the navigation bar
      let gradientImage44 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2))
      let gradientImage32 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2))
      
      // customize the appearance of UINavigationBar
      UINavigationBar.appearance().setBackgroundImage(gradientImage44, forBarMetrics: .Default)
      UINavigationBar.appearance().setBackgroundImage(gradientImage32, forBarMetrics: .Compact)
      UINavigationBar.appearance().barStyle = .Default
      
    2. Implement convenience methods to programmatically create UIImage objects.

      class func imageWithColor(colour: UIColor) -> UIImage {
          let rect = CGRectMake(0, 0, 1, 1)
      
          // Create a 1 by 1 pixel content
          UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
          colour.setFill()
          UIRectFill(rect)
      
          let image = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
      
          return image
      }
      
    0 讨论(0)
  • 2020-12-08 06:05

    As I support Colin's answer, I want to give you an additional hint to customize the appearance of an UINavigationBar including the alpha.

    The trick is to use UIAppearance for your NavigationBar. This enables you to assign an UIImage to your NavigationBar's backgroundImage. You can generate these UIImages programmatically and use for that UIColors and set the colors' alpha properties as you want. I've done this in one of my own applications and it works as expected.

    Here I give you some code snippets:

    1. E.g. in your ..AppDelegate.m add these lines in didFinishLaunchingWithOptions:

      //create background images for the navigation bar
      UIImage *gradientImage44 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for portrait orientation
      UIImage *gradientImage32 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for landscape orientation
      
      //customize the appearance of UINavigationBar
      [[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault];
      [[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];
      [[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
      
    2. Implement convenience methods to programmatically creates UIImage objects, e.g. create a new category for UIImage:

      //UIImage+initWithColor.h
      //
      #import <UIKit/UIKit.h>
      
      @interface UIImage (initWithColor)
      
      //programmatically create an UIImage with 1 pixel of a given color
      + (UIImage *)imageWithColor:(UIColor *)color;
      
      //implement additional methods here to create images with gradients etc.
      //[..]
      
      @end
      
      //UIImage+initWithColor.m
      //
      #import "UIImage+initWithColor.h"
      #import <QuartzCore/QuartzCore.h>
      
      @implementation UIImage (initWithColor)
      
      + (UIImage *)imageWithColor:(UIColor *)color
      {
          CGRect rect = CGRectMake(0, 0, 1, 1);
      
          // create a 1 by 1 pixel context 
          UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
          [color setFill];
          UIRectFill(rect);
      
          UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
      
          return image;
      }
      
    3. Re-work your image creation in 1. (#import "UIImage+initWithColor.h" in AppDelegate.m and replace the "nil"s):

    This is your spot of interest: by changing your colors' alpha property you are influencing the opacity level of you NavigationBar as well!

                UIImage *gradientImage44 = [UIImage imageWithColor:[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:0.2]];
                UIImage *gradientImage32 = [UIImage imageWithColor:[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:0.2]];
    

    I created a small demo project and add you two screenshots: the view itself has a yellow backgroundColor. The backgroundImages of the NavigationBar have a red color. Screenshot 1 shows a NavigationBar with a value for alpha = 0.2. Screenshot 2 shows a NavigationBar with a value for alpha = 0.8.

    Screenshot for NavigationBar with alpha=0.2 Screenshot for NavigationBar with alpha=0.8

    0 讨论(0)
  • 2020-12-08 06:13

    Swift3

    var navAlpha = // Your appropriate calculation   
    self.navigationController!.navigationBar.backgroundColor =  UIColor.red.withAlphaComponent(navAlpha)
    
    0 讨论(0)
  • 2020-12-08 06:15

    This should get you the effect you desire:

    self.navigationController.navigationBar.alpha = 0.0;
    self.navigationController.navigationBar.frame = CGRectMake(0,0,320,0);
    

    Did not try this in an animation, works in my viewDidAppear though, hope it works.

    0 讨论(0)
提交回复
热议问题