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

标签 ios swift uinavigationbar

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

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

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

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

enter image description here

最佳答案

根据我的经验,您还需要将 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()

}

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

= 真 enter image description here

=假 enter image description here

从马的嘴里可以这么说:

@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.
 */

关于ios - 更改 UINavigationBar 的半透明度具有相反的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865762/

相关文章:

ios - 为什么当我执行 removefromsuperview 时内存仍然存在?

swift - 在 Swift 中,编译器是否允许您隐式访问要打开的值?

ios - 我可以为警报 Controller 添加长按事件吗

iphone - 如何仅隐藏/禁用第一个 uinavigationbar?

ios - 将 Realm 与 Crashlytics 结合使用时的 NSUncaughtExceptionHandler

ios - 如何使用 AVURLAsset 流式传输视频并将缓存数据保存到磁盘

ios - 表格 View 中的下拉列表

swift - 什么可以触发 Firebase Auth 系统中的登录 session 过期

uinavigationbar - 如何去除 UINavigationBar 和 UISearchBar 细线

ios - 在导航栏下方设置 ImageView