iOS 11 navigationItem.titleView Width Not Set

后端 未结 14 1759
遥遥无期
遥遥无期 2020-12-13 07:58

Seeing a behavior on iOS11 with a navigationItem.titleView where the width of the titleView is not the full width of the screen.

I have a custom view that I set as t

14条回答
  •  清歌不尽
    2020-12-13 08:54

    Addition to the existing answers:

    If your custom title view is a view that already has an intrinsic content size by default (other than .zero), for example a UILabel, a UITextView or a UIButton, you can simply set

    yourCustomTitleView.translatesAutoresizingMaskIntoConstraints = false
    

    and it will automatically adjust to just enclose its contents, but never overlap with the left and right item views.


    For example, you can drag a button into the title view area of a navigation bar in Interface Builder, create an outlet titleButton for it in your view controller and then do

    override func viewDidLoad() {
        super.viewDidLoad()
        titleButton.translatesAutoresizingMaskIntoConstraints = false
    }
    

提交回复
热议问题