我正在尝试实现透明导航栏,在滚动表视图时逐渐变为纯色。为此,我将导航栏设置为透明并在其后面放置一个uiview(此UIView也将覆盖状态栏)。然后我可以在此视图上为alpha设置动画。但是,如果我按下一个新的视图控制器,然后尝试向后滑动过渡动画似乎很奇怪。右上角有一个黑色方块,如此(点击图片查看动画!):
代码是这样的:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
if !navCoverViewSet {
guard let navBarHeight = navBarHeight() else { return }
navCoverView.backgroundColor = UIColor.blue.withAlphaComponent(0.4)
navCoverView.translatesAutoresizingMaskIntoConstraints = false
navCoverView.heightAnchor.constraint(equalToConstant: navBarHeight).isActive = true
navCoverView.widthAnchor.constraint(equalToConstant: view.frame.size.width).isActive = true
navCoverView.bottomAnchor.constraint(equalTo: view.topAnchor, constant: 0)
view.addSubview(navCoverView)
navCoverViewSet = true
}
}
func navBarHeight() -> CGFloat? {
guard let navBarHeight: CGFloat = navigationController?.navigationBar.frame.size.height else { return nil }
let statusBarSize: CGSize = UIApplication.shared.statusBarFrame.size
let statusBarHeight: CGFloat = min(statusBarSize.width, statusBarSize.height)
return navBarHeight + statusBarHeight
}
为什么会发生这种情况的任何想法?我会喜欢这方面的建议。谢谢!