Perry Hoekstra
Perry Hoekstra

Reputation: 2763

Navigation Title does not center?

Quick question: the text of my Navigation Title does not center by default (unless I am running a 6 Plus), should it based on what type of phone it is displayed on? The title shows up centered in the storyboard (using the Universal storyboard size), however, if I look on Preview or the actual phone (4S, 5, and 5S), the title is pushed all the way to the right. Do I actually have to attempt to use AutoLayout or is there a setting in the Attribute Settings that I am missing?

enter image description here

Upvotes: 1

Views: 7399

Answers (4)

Dayanithi Natarajan
Dayanithi Natarajan

Reputation: 843

CGRect frame = CGRectMake(0, 0, 0, 44);
UILabel *label = [[UILabel alloc]initWithFrame:frame];
label.text = @"Title Here";
self.navigationItem.titleView = label;

Upvotes: 5

fujianjin6471
fujianjin6471

Reputation: 5248

Without Auto Layout, the UI will appear in device/Simulator at exactly the same position as in storyboard, measured in points.

In storyboard, the scene is 600*600, and the title centers horizontally, so the horizontal ordinate of the title's center is 300, maybe at letter 'p'. Any iPhone earlier than 6/6Plus has the width of 320, as you can see in your screen shot, the right edge ends a little farther than 'p'.

Auto Layout can help you and in my experience it pays off to grasp it.

First, select Navigation Bar, then in the menu, choose Editor->Align->Horizontal Center in Container, that's all you have to do.

You will notice a warning arises, it won't bother you. You can ignore it until you understand Auto Layout.

I highly recommend this tutorial

Upvotes: 1

Sategroup
Sategroup

Reputation: 955

I faced the same issue and it's the size classes next to Auto Layout that is causing the issue. If you disable it, it would appear centered. I am yet to find how to fix it with size classes on

Upvotes: 0

Casey Fleser
Casey Fleser

Reputation: 5787

It may be that you have wide bar button item/s in which case the title will be pushed off-center. For example this longish button name on an iPhone 4s pushes the title to the left. The same title / button name fits comfortably on a 6+ and centers as expected.

enter image description here

Upvotes: 1

Related Questions