Change the alpha value of the navigation bar

后端 未结 9 1694
误落风尘
误落风尘 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 05:59

    You can also try to set a background view underneath the navigation bar and modify this view directly.

    private lazy var backgroundView: UIView = UIView()
    
    ...
    
    private func setupNavigationBarBackground() {
        guard let navigationBar = navigationController?.navigationBar else { return }
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.isTranslucent = true
        view.addSubview(backgroundView)
        backgroundView.alpha = 0
        backgroundView.backgroundColor = .red
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        backgroundView.topAnchor.constraint(equalTo: topLayoutGuide.topAnchor).isActive = true
        backgroundView.bottomAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
        backgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        backgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    }
    
    private func changeNavigationBarAlpha(to alpha: CGFloat) {
        backgroundView.alpha = alpha
    }
    

提交回复
热议问题