导航栏颜色不在状态栏下方。

3
我正在尝试制作一个透明的导航栏,但当我将其设置为透明时,它看起来像这样:transparent
我希望它看起来像这样non-transparent,但是具有类似于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 - keithbhunter
它嵌入了一个UINavigationController。 - Phyber
你在一个UINavigationController中嵌入了一个自定义的UINavigationController?为什么这样做?我们能看到自定义UINavigationController的代码吗? - keithbhunter
类:https://imgur.com/a/NAd6p,故事板:https://imgur.com/a/2AgcM - Phyber
1个回答

4
让我们把这个问题分成几个部分。首先,您需要使用使用带有UIBlurEffectUIVisualEffectView来获得所需的模糊/透明效果。然后,您需要找出如何在导航栏中显示它,以便它填充整个导航栏和状态栏。
第一部分
我创建了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
    }

}

第二部分

现在我们需要在导航栏中使用这个视图。我是在一个嵌入了UINavigationControllerUIViewController中完成的。

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
}

以下是模拟器结果的图片。你可以看到在图片左上方有一些模糊的文字,其中状态栏白色部分看起来更暗。 模糊/透明导航栏

非常好,谢谢! - Phyber
在iPhoneX上硬编码“-20”可能是一个坏主意。 - Tony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接