Adding autolayout constraints to navigation controller programmatically

前端 未结 3 943
闹比i
闹比i 2020-12-21 03:50

I am trying to add constraints to a UI label I added on a navigation bar. The UI label shows a running timer

Here is my code,

self.label = [[UILabel          


        
相关标签:
3条回答
  • In short: you can't.

    The navigation controller manages the creation, configuration, and display of the navigation bar and optional navigation toolbar. It is permissible to customize the navigation bar’s appearance-related properties but you must never change its frame, bounds, or alpha values directly ... A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack

    Source: Apple Docs

    You would need to subclass the UINavigationBar and then instantiate the UINavigationController using initWithNavigationBarClass:toolbarClass: to do something further.

    Though if you're looking for ways to customize the navbar, UIKit User Interface Guide is a good start to know what you may be able to do. But if it's going to be a text label, consider setting the bar's title property, for example.

    0 讨论(0)
  • 2020-12-21 04:35

    I ended up adding the timer label as a right button bar item on the navigation bar. It is a hacky way to do it. But since adding constraints is not possible, it is a workaround.

    0 讨论(0)
  • 2020-12-21 04:38

    Another way, after adding your custom view as a subview to UINavigationBar, manually setup position and size with an old fashion way by updating .frame property. It isn't a "flexible" solution, but simple and fast.

    0 讨论(0)
提交回复
热议问题