Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UINavigationBar with custom height causes the UIBarButtonItem's to be positioned wrong

I created my own subclass of UINavigationBar in order to enable custom background that is taller than 44pxs.

I did it by overriding these two methods:

-(void) drawRect:(CGRect)rect
{
    [self.backgroundImage drawInRect:CGRectMake(0, 0, self.backgroundImage.size.width, self.backgroundImage.size.height)];
}

- (CGSize)sizeThatFits:(CGSize)size 
{
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height);
    return newSize;
}

And this is the result:

Custom bar with custom height

Now, my problem as you can see is that all the UIBarButtonItem's (and the titleView) are placed at the bottom of the navigation bar.

I would like them to be pinned to the top of the bar (with some padding of course). What to I need to override to achieve that?

Thanks!

EDIT:

This is the solution that I used:

-(void) layoutSubviews
{
    [super layoutSubviews];
    for (UIView *view in self.subviews)
    {
        CGRect frame = view.frame;
        frame.origin.y = 5;
        view.frame = frame;
    }
}

Does the trick for idle state, still have some weird behavior on push and pop items.

like image 476
Avi Shukron Avatar asked Jan 04 '12 11:01

Avi Shukron


2 Answers

Try to override layoutSubviews: call [super layoutSubviews] inside and then reposition the items.

like image 174
Michał Zygar Avatar answered Sep 30 '22 03:09

Michał Zygar


To solve the push/pop issue use setTitleVerticalPositionAdjustment in sizeThatFits:(CGSize)size

- (CGSize)sizeThatFits:(CGSize)size {
    UIImage *img = [UIImage imageNamed:@"navigation_background.png"];
    [self setTitleVerticalPositionAdjustment:-7 forBarMetrics:UIBarMetricsDefault];
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize newSize = CGSizeMake(frame.size.width , img.size.height);
    return newSize;
}
like image 41
Wyeth Shamp Avatar answered Sep 30 '22 02:09

Wyeth Shamp