简体   繁体   English

UINavigationItem TitleView 消失

[英]UINavigationItem TitleView disappears

I am trying to create a custom titleView for a navigation bar.我正在尝试为导航栏创建自定义 titleView。 I am able to set the titleView in the root view controller that is embedded in a navigation controller.我能够在嵌入在导航控制器中的根视图控制器中设置 titleView。

When I push the second view controller onto the stack and try to set the titleView for this view controller it does not work.当我将第二个视图控制器推入堆栈并尝试为该视图控制器设置 titleView 时,它不起作用。 The titleView quickly appears and disappears. titleView 快速出现和消失。 When I go back to the previous view controller this titleView quickly appears and disappears now also.当我回到上一个视图控制器时,这个 titleView 现在也迅速出现和消失。

Does anyone know why this is happening or how to set the titleView correctly without flashing and disappearing?有谁知道为什么会发生这种情况或如何正确设置 titleView 而不闪烁和消失?

class FirstViewController: UIViewController {

    var titleView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        addTitleView()
    }

    func addTitleView() {
        titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))

        let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
        companyLabel.text = "CPS Dashboard"
        companyLabel.textColor = UIColor.grayColor()
        companyLabel.textAlignment = .Center
        companyLabel.font = UIFont.systemFontOfSize(9)
        titleView.addSubview(companyLabel)

        let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
        titleLabel.text = "Dashboard"
        titleLabel.textColor = UIColor.blackColor()
        titleLabel.textAlignment = .Center
        titleLabel.font = UIFont.systemFontOfSize(15)
        titleView.addSubview(titleLabel)

        navigationItem.titleView = titleView
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Show" {
            let controller = segue.destinationViewController as! SecondViewController
            controller.titleView = titleView
        }
    }
}

The second viewcontroller:第二个视图控制器:

class SecondViewController: UIViewController {

    var titleView: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let titleView = titleView {
            navigationItem.titleView = titleView
        }
    }
}

I found a solution.我找到了解决方案。 I copied addTitleView() method from FirstViewController into SecondViewController, and called both of them in viewDidLoad().我将 addTitleView() 方法从 FirstViewController 复制到 SecondViewController,并在 viewDidLoad() 中调用它们。 This worked exactly as I wanted it to.这完全按照我的意愿工作。 For some reason it was not working to pass the titleView forward as a property and assigning it to navigationItem.titleView.由于某种原因,它无法将 titleView 作为属性向前传递并将其分配给 navigationItem.titleView。

class FirstViewController: UIViewController {

    var titleView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        addTitleView()
    }

    func addTitleView() {
        titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))

        let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
        companyLabel.text = "CPS Dashboard"
        companyLabel.textColor = UIColor.grayColor()
        companyLabel.textAlignment = .Center
        companyLabel.font = UIFont.systemFontOfSize(9)
        titleView.addSubview(companyLabel)

        let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
        titleLabel.text = "Dashboard"
        titleLabel.textColor = UIColor.blackColor()
        titleLabel.textAlignment = .Center
        titleLabel.font = UIFont.systemFontOfSize(15)
        titleView.addSubview(titleLabel)

        navigationItem.titleView = titleView
    }
}

The second viewcontroller:第二个视图控制器:

class SecondViewController: UIViewController {

    var titleView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        addTitleView()
    }

    func addTitleView() {
        titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))

        let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
        companyLabel.text = "CPS Dashboard"
        companyLabel.textColor = UIColor.grayColor()
        companyLabel.textAlignment = .Center
        companyLabel.font = UIFont.systemFontOfSize(9)
        titleView.addSubview(companyLabel)

        let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
        titleLabel.text = "Dashboard"
        titleLabel.textColor = UIColor.blackColor()
        titleLabel.textAlignment = .Center
        titleLabel.font = UIFont.systemFontOfSize(15)
        titleView.addSubview(titleLabel)

        navigationItem.titleView = titleView
    }
}

My solution is simple, and it works:我的解决方案很简单,而且有效:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let tv = navigationItem.titleView {
        print("transform", tv.transform)) // is always identity
        let bounds = tv.bounds
        print("bounds", bounds) // its origin may not be zero.
        tv.bounds = CGRect(origin: .zero, size: bounds.size)
        print("new bounds", tv.bounds)
    }
}

Using Xcode's view debugger, you will find that titleView.bounds.origin is not zero.使用 Xcode 的视图调试器,您会发现 titleView.bounds.origin 不为零。
How to let it happen again , two steps: 1. UIViewController A and B;如何让它再次发生,两步: 1. UIViewController A 和 B; A has custom navigationItem.titleView , B hides navigationBar in its viewWillAppear() ; A 有自定义的navigationItem.titleView ,B 在其viewWillAppear()隐藏了navigationBar when B poped, A.viewWillAppear() setNavigationBar(hidden: false, animated: true) 2. user-driven popViewController is canceled by lifting your hand.当 B 弹出时, A.viewWillAppear() setNavigationBar(hidden: false, animation: true) 2. popViewController取消用户驱动的popViewController
Then you will found, A's navigationBar is blank.然后你会发现,A的navigationBar是空白的。

I was having this same issue, but none of the above solutions fixed it for me.我遇到了同样的问题,但上述解决方案都没有为我解决这个问题。 My issue was that I was setting translatesAutoresizingMaskIntoConstraints to false .我的问题是我将translatesAutoresizingMaskIntoConstraints设置为false I imagine this caused the appearing/disappearing because it needs to be set to true in order to constrain the view internally to the navigation bar.我想这会导致出现/消失,因为需要将其设置为true才能将视图内部限制为导航栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM