【问题标题】:Navigation bar color is not under status bar导航栏颜色不在状态栏下方
【发布时间】:2016-08-23 18:29:28
【问题描述】:

我正在尝试制作一个透明的导航栏,但是当我将其设置为透明时,它看起来像这样......:

我希望它看起来像这样

但像 App Store 中一样透明和模糊,但带有背景颜色。问题是导航控制器的背景颜色不像正常的那样在状态栏下。

我的代码:

 self.navigationItem.title = "label"
 self.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
 self.navigationBar.shadowImage = UIImage()
 self.navigationBar.isTranslucent = true
 self.navigationBar.backgroundColor = UIColor.init(red: 255/255, green: 0, blue: 0, alpha: 0.7)

编辑:我有一个 UINavigationController 的自定义类,视图控制器嵌入在 UINavigationController

Swift 3、Xcode 8.0 beta 5。

【问题讨论】:

  • 您的视图控制器是嵌入在UINavigationController 中还是在视图上添加了UINavigationBar 作为子视图?
  • 它嵌入在 UINavigationController 中
  • 您在UINavigationController 中嵌入了自定义UINavigationController?为什么?我们可以看到自定义UINavigationController 的代码吗?
  • 班级:imgur.com/a/NAd6p,情节提要:imgur.com/a/2AgcM

标签: ios swift uinavigationcontroller uinavigationbar swift3


【解决方案1】:

让我们把这个问题分解成几个部分。首先,您需要使用由UIBlurEffect 创建的UIVisualEffectView 来获得您想要的模糊/透明效果。然后你需要弄清楚如何在导航栏中显示它,以便它填满整个导航栏和状态栏。

第 1 部分

我创建了UIVisualEffectView 的子类以添加渐变。我们可以使用此视图来创建所需的模糊/透明效果。

class GradientVisualEffectView: UIVisualEffectView {

    private let gradient: CAGradientLayer = {
        // You can tweak these colors and their alpha to get the desired gradient.
        // You can also mess with the gradient's locations.
        $0.colors = [
            UIColor.white.withAlphaComponent(0.3).cgColor, 
            UIColor(red: 1, green: 0, blue: 0, alpha: 0.7).cgColor
        ]
        return $0
    } (CAGradientLayer())


    override init(effect: UIVisualEffect?) {
        super.init(effect: effect)
        layer.addSublayer(gradient)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // Make sure the gradient's frame always matches the blur effect.
        gradient.frame = bounds
    }

}

第 2 部分

现在我们需要在导航栏中使用这个视图。我在嵌入在UINavigationController 中的UIViewController 中执行此操作。

override func viewDidLoad() {
    super.viewDidLoad()

    // Remove the nav bar's background
    let navBar = navigationController!.navigationBar
    navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    navBar.backgroundColor = .clear

    // Create the blur/transparent view. You can mess with styles here to get
    // different effects.
    let gradientBlur = GradientVisualEffectView(effect: UIBlurEffect(style: .light))
    gradientBlur.translatesAutoresizingMaskIntoConstraints = false
    navBar.addSubview(gradientBlur)

    // Constrain the view so that it always matches the nav bar.
    // The top constraint has a -20 constant so that it will extend above
    // the nav bar to the status bar.
    gradientBlur.leftAnchor.constraint(equalTo: navBar.leftAnchor).isActive = true
    gradientBlur.topAnchor.constraint(equalTo: navBar.topAnchor, constant: -20).isActive = true
    gradientBlur.rightAnchor.constraint(equalTo: navBar.rightAnchor).isActive = true
    gradientBlur.bottomAnchor.constraint(equalTo: navBar.bottomAnchor).isActive = true
}

下面是我的模拟器的结果图片。您可以在图片的左上方看到一些模糊的文字,其中状态栏的白色部分看起来更暗。

【讨论】:

  • 效果很好。谢谢!
  • 硬编码-20 在 iPhoneX 上中断,这可能是个坏主意
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 2019-04-20
相关资源
最近更新 更多