I am trying to add a custom view in the center of a navigation bar and I am using the following code to test it:
UIView * testView = [[UIView alloc] init];
[testView setBackgroundColor:[UIColor blackColor]];
testView.frame = CGRectMake(0, 0, 100, 35);
[self.navigationController.navigationItem.titleView addSubview:testView];
I am setting this up in the viewDidLoad method of my view controller but when i run my program nothing seems to change in my navigation bar.
Could you help me with this?
This works. Give frame at the time of initialisation
Swift 3/4
You may set i.e.
UILabel
as atitleView
. Call it inviewDidLoad()
:Note
navigationItem.title = nil
, otherwisetitle
may overridetitleView
.