Customize navigation bar with title view

Julian Osorio picture Julian Osorio · Dec 8, 2011 · Viewed 84.8k times · Source

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?

Answer

virata picture virata · Dec 8, 2011

This works. Give frame at the time of initialisation

 UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
 [iv setBackgroundColor:[UIColor whiteColor]];
  self.navigationItem.titleView = iv;