【发布时间】:2020-02-29 14:35:42
【问题描述】:
我有两个视图控制器,我正在使用 push segue 来显示它们。我的故事板如下所示:
但我想在第二个视图控制器上使用独立导航栏。为此,我编写了以下代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
placeNavigationBar()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
var normalButton: UIButton!
var counter = 0
let item = UINavigationItem()
let navigationBar = UINavigationBar()
private func placeNavigationBar() {
let backButtonImage = UIImage(named: "backButton")
normalButton = UIButton(frame: CGRect(x: 0, y: 0, width: backButtonImage!.size.width, height: backButtonImage!.size.height))
normalButton.setImage(backButtonImage, for: .normal)
normalButton.setTitleColor(.systemBlue, for: .normal)
normalButton.contentHorizontalAlignment = .leading
normalButton.contentVerticalAlignment = .center
normalButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 3.0, bottom: 0.0, right: -3.0)
normalButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
let backBarButton = UIBarButtonItem(customView: normalButton)
backBarButton.tintColor = .systemBlue
let leftLabel = UILabel()
leftLabel.text = "anony-12345678645"
leftLabel.textColor = .black
let profilePicture = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
profilePicture.translatesAutoresizingMaskIntoConstraints = false
profilePicture.image = #imageLiteral(resourceName: "test2")
profilePicture.contentMode = .scaleAspectFill
profilePicture.clipsToBounds = true
profilePicture.widthAnchor.constraint(equalToConstant: 30).isActive = true
profilePicture.heightAnchor.constraint(equalToConstant: 30).isActive = true
profilePicture.layer.cornerRadius = 15
item.leftBarButtonItems = [backBarButton, UIBarButtonItem(customView: profilePicture) ,UIBarButtonItem(customView: leftLabel)]
navigationBar.isTranslucent = false
navigationBar.barTintColor = .white
self.view.addSubview(navigationBar)
navigationBar.translatesAutoresizingMaskIntoConstraints = false
navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
navigationBar.items = [item]
navigationBar.delegate = self
}
它运行良好,但我对导航栏过渡动画不满意。该视频展示了它现在的样子:
https://www.youtube.com/watch?v=1wakUhA940c
我想要什么:
检查两个视频的导航栏转换,在第二个视频中,导航栏位于第一个视图控制器导航栏的顶部。但在第一个视频中,第二个导航栏只是将第一个导航栏推到屏幕外。
如何在第二个视频中实现相同的转换?
【问题讨论】: