views:

207

answers:

2

I am trying to add a custom control as the titleView in a UINavigationBar. When I do so, despite setting the frame and the properties that would normally assume full width, I get this: alt text

The bright blue can be ignored as it is where I am hiding my custom control. The issue is the narrow strips of navbar at the ends of the bar. How can I get rid of these so my customview will stretch 100%?

CGRect frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.width, kDefaultBarHeight);
UANavBarControlView *control = [[[UANavBarControlView alloc] initWithFrame:frame] autorelease];
control.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
self.navigationItem.titleView = control;

PS - I know I can add the view by itself instead of being attached to a navigation bar and it would be very easy to position it myself. I have my reasons for needing it to be "on" the navigation bar, and those reasons are here

A: 

Setting the titleView of your view's navigationItem will never does the trick. Instead, you can add a subView to the navigation controller's navigationBar :

UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds];
ctrl.backgroundColor = [UIColor yellowColor];
ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[navController.navigationBar addSubview:ctrl];
VdesmedT
This approach doesn't work well with navigation. Pushing a viewcontroller on the stack leves it here. I could remove it before the pushes and pulls, but this seems very hacky
coneybeare
I ended up hacking it to work by adding animations to add/remove it on viewwillappear and viewwilldissapear. A hacky solution for a hacky question I guess. I will hold off on giving you the bounty to see if a better solution appears.
coneybeare
That's what I did as well. As you say : hacky solution !
VdesmedT
Have you tried subclassing UINavigationController to change the frame of the titleview internally ? I have no idea how to do it though !
VdesmedT
A: 

CGRect frame = CGRectMake(0, 0, 320, 44);

UILabel *titlelabel = [[UILabel alloc]initWithFrame:frame];

titlelabel.textAlignment = UITextAlignmentCenter;

titlelabel.backgroundColor = [UIColor clearColor];

titlelabel.textColor = [UIColor whiteColor];

titlelabel.font = [UIFont systemFontOfSize:20];

titlelabel.text =@"Available Reports";

self.navigationItem.titleView = titlelabel;

if you want to set image then take uiimage view instead on uilable you can create any view of fully navigation bar just tell me how ur navigation look like i will send you code for that if you want i can put 6 button on navigation also

GhostRider
I guess you didn't even try that before posting did you ?
VdesmedT
this only appears to work for you because you have a clear background.
coneybeare