How to place UILabel in the center of navigation bar
In Xcode 6
if you want to put a label inside the UINavigationBar
firstly you have to put a UIView there then put the UILabel inside the UIView (this is from the Storyboard btw).
If you do not put the UIView
first then the UILabel
will never get put onto the UINavigationBar
.
Create a UIView and add UILabel
as it's subview and then set your NavigationItem
's titleView as previously created UIView.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];label.text = @"Hello";label.textAlignment = NSTextAlignmentCenter;[view addSubview:label];self.navigationItem.titleView = view;
Note: Don't forget to set your own frame values to get better result.
Just make sure you try viewWillLayoutSubviews
method instead of viewDidLoad
that was what worked for me. Cheers!