Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place UILabel in the center of navigation bar

How to place UILabel in the center of navigation bar in XCode 6? Is it possible at all? I can place here, for example, UIButton, but unable to place UILabel. If no, what can I do then? Place a UIButton with the appropriate text and make it non-clickable?

Thanks in advance.

like image 752
FrozenHeart Avatar asked Dec 04 '14 14:12

FrozenHeart


1 Answers

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.

like image 147
Ersin Sezgin Avatar answered Oct 20 '22 00:10

Ersin Sezgin