Can't set titleView in the center of navigation bar because back button

前端 未结 10 1547
情话喂你
情话喂你 2020-12-08 02:25

I\'m using an image view to display an image in my nav bar. The problem is that I can\'t set it to the center correctly because of the back button. I checked the related que

10条回答
  •  隐瞒了意图╮
    2020-12-08 02:47

    Extending Darren's answer, a fix for me was to return a sizeThatFits with the UILabel size. It turns out that this is called after layoutSubViews so the label has a size.

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: titleLabel.frame.width + titleInset*2, height: titleLabel.frame.height)
    }
    

    Also note that I have + titleInset*2 because Im setting the horizontal constraints like so:

    titleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: titleInset),
    titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -titleInset)
    

提交回复
热议问题