I noticed that the titleView\'s position depends on the rightbarbutton title. How can I set the frame of the titleView to be in the center instead?
I\'ve tried setting
Sometime in between your view controller's viewWillAppear: & viewDidAppear:, the navigation bar is repositioning your titleView and resizing it to fit if necessary. It can end up uncentered because the navigation bar is prefering not to resize your titleView to fit between the buttons. It seems to try to center the title view if it's small enough, but if not will move your titleView off-center, and then I think only as last resort will resize. The effect is that the titleView is taking up all the space between the two buttons, if its textAlignment is centered then it will centered in that space though not the centered with respect to the whole screen. You can see this in your screen shot.
One answer is to make your titleView narrower so the navigation bar can center it, so try about 160 instead of 180 when you set your titleView.frame. It sucks that, when creating the view programmatically like you are, one has to put an estimate for that width into the code. What would be good is a way to maximize that space while staying centered.
It's possible to start with the titleView being the screen width, then after the navigation bar changes titleView.frame, update it again yourself in the view controller's viewDidAppear:. Using its adjusted titleView.frame.origin.x and size.width along with the screen width, you can calculate the largest of the left & right margins, then set the origin.x to that, the size.width to the screen width minus that times 2. However that doesn't take effect until after the pushed view has animated in and the shift afterwards is visible. You could hide the titleView in viewWillAppear: then unhide in viewDidAppear: after centering it, so instead of sliding in with an off-center titleView which is then shifted, it would slide in with no title which then appears.
A better possibility is to make your titleView your own subclass of UIView (or UILabel or whichever) and override setFrame to resize itself to stay centered in its superview. If I ever end up doing this myself I'll post it here. Or maybe someone else knows another place to change the titleView's frame after its been updated but before the view slides in without a view subclass.
My solution was to manually resize the navigation bar in viewDidAppear:
- (void)viewDidAppear
{
[super viewDidAppear];
[self.navigationController.navigationBar setFrame:CGRectMake(x, y, width, height)];
}
...after that you can reposition the title view as well. I just noticed that the main issue was the fact that the navigation bar wqas resized as well, so I brought it back to its regular size. I got the regular size from debugging and checking its size in viewDidLoad before it got modified...
You can try set the frame to CGRectZero initially and call sizeToFit. Below is the code I used to change the title text and made sure it is still centered. I had a back button on the left.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"Title";
[label sizeToFit];
self.navigationItem.titleView = label;
Recently I face with the same problem, I have a complex title view whose contents may changes, so I want can show as more content as possible, my left bar button item and right bar button item has different width, so if I set it a long width, the navigation bar won't center it. As you may know when the left or right bar button item is very long, it's impossible to center the title view, so I want to center it as much as possible.
Once we set a view to self.navigationItem.titleView
, the navigation bar will manage title view's frame, it's hard to center it directly, so I subclass a view which works as the real title view's container view, I set its width with the screen width, UINavigationBar
with set its frame to a property value respecting to its left/right bar button item.
The container view is MDLNavigationTitleContainer
and the real title view is MDLMessagesNavigationTitleView
, MDLMessagesNavigationTitleView
is managed by auto layout, as it's super view is MDLNavigationTitleContainer
, every time its width changes, layoutSubviews
will be called. The code in layoutSubviews
seems a little weird, it is used to handle the push/pop animation.
@interface MDLNavigationTitleContainer ()
@property (nonatomic) CGRect oldFrame;
@end
@implementation MDLNavigationTitleContainer
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UINib *nib = [UINib nibWithNibName:@"MDLMessagesNavigationTitleView" bundle:[NSBundle bundleForClass:[MDLMessagesNavigationTitleView class]]];
self.titleView = [[nib instantiateWithOwner:nil options:nil] firstObject];
self.titleView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.titleView];
NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
width.priority = 200;
NSLayoutConstraint *width1 = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[self addConstraint:centerX];
[self addConstraint:centerY];
[self addConstraint:width];
[self addConstraint:width1];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (!self.superview) {
return;
}
if (self.center.x > [UIScreen mainScreen].bounds.size.width) {
self.titleView.frame = self.oldFrame;
return;
}
CGFloat centerX = self.center.x;
CGFloat superWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat superCenterX = superWidth/2;
CGFloat offsetX = superCenterX - centerX;
CGPoint center = self.titleView.center;
if (CGRectGetMaxX(self.titleView.frame) + offsetX < self.bounds.size.width &&
CGRectGetMinX(self.titleView.frame) + offsetX > 0) {
center = CGPointMake(self.bounds.size.width/2 + offsetX, self.bounds.size.height/2);
}
CGSize size = self.titleView.bounds.size;
if (size.width > self.bounds.size.width) {
size.width = self.bounds.size.width;
}
self.titleView.frame = CGRectMake(center.x-size.width/2, center.y-size.height/2, size.width, size.height);
self.oldFrame = self.titleView.frame;
}
Set the title view:
MDLNavigationTitleContainer *titleView = [[MDLNavigationTitleContainer alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
self.navigationItem.titleView = titleView;
Same problem here. Implementing autoscrolling label as titleView. So that long text in navigation title could be scrolled automatically for user to see it all. All that problems with unknown real dimensions of titleView prevents text centering in simplest special case: when label text fits and no autoscrolling needed (like setting text to title property of navigationItem).
Just add invisible button (no text) on right side of navigation bar, and change it size to for example 20 px