Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a full-sized UINavigationBar titleView

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

like image 908
coneybeare Avatar asked Sep 20 '10 23:09

coneybeare


2 Answers

Just ran into the same problem and after a bit of thinking solved it. titleView's frame gets set by the navigationBar every time it appears.

All you have to do is subclass UIView and override setFrame.

Like this:

    - (void)setFrame:(CGRect)frame {
        [super setFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)];
    }

Now set your sneaky new UIView as the navigationItem.titleView and enjoy its newfound resistance to resizing by the superview.

You don't have to set super's frame every time your frame gets set. You can just set it once and be done. If you want to support orientation changes you could probably hack that together too.

like image 131
ksm Avatar answered Oct 24 '22 18:10

ksm


Setting the titleView of your view's navigationItem will never do 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];
like image 30
VdesmedT Avatar answered Oct 24 '22 18:10

VdesmedT