4

I am trying to create a custom titleView for a navigation bar. I am able to set the titleView in the root view controller that is embedded in a navigation controller.

When I push the second view controller onto the stack and try to set the titleView for this view controller it does not work. The titleView quickly appears and disappears. When I go back to the previous view controller this titleView quickly appears and disappears now also.

Does anyone know why this is happening or how to set the titleView correctly without flashing and disappearing?

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
        }
    }
}
vrwim
  • 13,020
  • 13
  • 63
  • 118
atmospherelabs
  • 163
  • 3
  • 10

3 Answers3

2

I found a solution. I copied addTitleView() method from FirstViewController into SecondViewController, and called both of them in 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.

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
    }
}
vrwim
  • 13,020
  • 13
  • 63
  • 118
atmospherelabs
  • 163
  • 3
  • 10
  • 2
    A view can only be added to one superview. When you add a view as a subview of a different superview, it will disappear from the old one. So most likely the code of your 2 viewcontrollers had a little fight about who should be responsible for displaying the title view. – Thyraz Nov 23 '15 at 16:19
  • I see thank you for that explanation that is probably what was happening – atmospherelabs Nov 23 '15 at 16:22
0

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.
How to let it happen again, two steps: 1. UIViewController A and B; A has custom navigationItem.titleView, B hides navigationBar in its viewWillAppear(); when B poped, A.viewWillAppear() setNavigationBar(hidden: false, animated: true) 2. user-driven popViewController is canceled by lifting your hand.
Then you will found, A's navigationBar is blank.

DawnSong
  • 4,752
  • 2
  • 38
  • 38
0

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. 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.

NiltiakSivad
  • 51
  • 1
  • 4