I'm switching over to use auto layout, programatically, so it's all new to me. As a general rule, I think I should be avoiding setting any frames directly...
Problem: I want to use a UILabel
as the view for my view controller's UINavigationItem
titleView
.
Is it possible to use auto layout constraints with the navigation bar / navigation item? Or, do we still need to set the frame directly in some circumstances?
E.g. I'm doing something like this:
UILabel* title = [UILabel new];
title.text = @"Some Title";
[title sizeToFit];
CGFloat titleWidth = title.bounds.size.width;
[title setFrame:CGRectMake(0, 0, titleWidth, 32.0)];
self.navigationItem.titleView = title;
As you can see, I've set the height of that view to 32.0
(for landscape mode). Further, I allow the title text to change, so I re-do the sizeToFit
and setFrame
if the user makes a change.
Is this the way to do it? Or, is there an auto layout way?
Note: I have looked at this question/answer: Building a titleView programmatically with constraints (or generally constructing a view with constraints)
Reading through that - I'm still not sure if it's possible to use constraints only?