Change the alpha value of the navigation bar

后端 未结 9 1709
误落风尘
误落风尘 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 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
      }
      

提交回复
热议问题