Adding autolayout constraints to navigation controller programmatically
Asked Answered
G

3

5

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 alloc] initWithFrame:CGRectMake(550, 15, 150, 14)];
self.label.text = @"Label";
[self.label setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.navigationController.navigationBar addSubview:self.label];

[self.navigationController.navigationBar addConstraint:[NSLayoutConstraint constraintWithItem:self.label
                                                       attribute:NSLayoutAttributeTrailing
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self.navigationController.navigationBar
                                                       attribute:NSLayoutAttributeTrailing
                                                      multiplier:1.0f
                                                        constant:-20.0f]];

But I get the error 'Cannot modify constraints for UINavigationBar managed by a controller' when I run the app.

How do I add this constraint?

Giannagianni answered 24/2, 2015 at 23:2 Comment(0)
R
9

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.

Reathareave answered 24/2, 2015 at 23:22 Comment(1)
I already have a title. I have to add a label that shows a timer on the navigation bar. The only workaround is subclassing the nav bar?Giannagianni
Q
1

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.

Quantize answered 28/3, 2017 at 7:9 Comment(0)
G
0

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.

Giannagianni answered 26/2, 2015 at 19:23 Comment(1)
Can't you just create simple titleView and add it to the navbar like this: self.navigationItem.titleView = titleView; ?Affenpinscher

© 2022 - 2024 — McMap. All rights reserved.