更改 UINavigationBar 的半透明度具有相反的效果

Changing the translucency of a UINavigationBar has the opposite effect

我有一个非常简单的设置,一个初始导航控制器,一个简单的视图控制器作为根。我在这里更改了导航栏的颜色。

   override func viewDidLoad() {
      super.viewDidLoad()
      makeNavigationBar()
   }

   func makeNavigationBar() {
      guard let navigationBar = navigationController?.navigationBar else {
         return
      }
      navigationBar.barTintColor = UIColor.someColor()
      navigationBar.translucent = true
   }

奇怪的是 - 改变 translucent 属性 似乎有相反的效果,如图所示。为什么会这样?我应该怎么做才能像宣传的那样工作?

根据我的经验,您还需要将 navigationBar.barStyle 设置为 UIBarStyle.BlackTranslucent

代码

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.navigationController?.navigationBar.barStyle       = UIBarStyle.BlackTranslucent
    self.navigationController?.navigationBar.translucent    = false
    self.navigationController?.navigationBar.barTintColor   = UIColor.whiteColor()

}

切换时的结果true/false 我用白色和灰色来夸大效果

=真

=假

从马口中可以这么说:

@availability(iOS, introduced=2.0)
class UINavigationBar : UIView, NSCoding, UIBarPositioning,   NSObjectProtocol {

var barStyle: UIBarStyle
unowned(unsafe) var delegate: UINavigationBarDelegate?

/*
 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.
 */
@availability(iOS, introduced=3.0)
var translucent: Bool // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent

// Pushing a navigation item displays the item's title in the center of the navigation bar.
// The previous top navigation item (if it exists) is displayed as a "back" button on the left.
func pushNavigationItem(item: UINavigationItem, animated: Bool)
func popNavigationItemAnimated(animated: Bool) -> UINavigationItem? // Returns the item that was popped.

var topItem: UINavigationItem? { get }
var backItem: UINavigationItem? { get }

var items: [AnyObject]!
func setItems(items: [AnyObject]!, animated: Bool) // If animated is YES, then simulate a push or pop depending on whether the new top item was previously in the stack.

/*
 The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background
 and behaves as described for the tintColor property added to UIView.
 To tint the bar's background, please use -barTintColor.
 */
var tintColor: UIColor!
@availability(iOS, introduced=7.0)
var barTintColor: UIColor? // default is nil

/* In general, you should specify a value for the normal state to be used by other states which don't have a custom value set.

 Similarly, when a property is dependent on the bar metrics (on the iPhone in landscape orientation, bars have a different height from standard), be sure to specify a value for UIBarMetricsDefault.
 */

试试这个

navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationBar.shadowImage = UIImage()
navigationBar.translucent = true

最后发现真正的问题是我的主视图的背景颜色。我已经使用 Interface Builder 的颜色选择器手动设置了它的 RGB 值。虽然我会以编程方式使用 UIColor 设置导航栏的颜色。我不知道为什么它们会产生不同的结果,但在我更改了选择器中的颜色配置文件后,现在一切都匹配了。

澄清一下:

  • 之前的值为 sRGB
  • 新的,正确的值为通用 RGB