Drawing in custom UINavigationBar, attached to top

2019-08-10 02:42发布

问题:

I have a subclass of UINavigationBar. It's barPosition is UIBarPositionTopAttached. Than I override -(void)drawRect:(CGRect)rect in my subclass. The rect, that comes as parameter, has always 44 px height, and I can draw only inside this rect. So, I can't perform drawing over the status bar and it has default look. If I comment -drawRect out, than it looks as expected, navigation bar and status bar look as a whole and have 64 px height. Is there a way to achieve this effect with having overrided -drawRect: in subclass of UINavigationBar?

回答1:

I had a the issue of a custom UINavigationBAr with a background image which would not appear under the translucent Status Bar on iOS 7 when I was adding the image in drawRect.

After moving the background image to the ViewControllers viewDidLoad Method, the background image then appeared under the Status Bar

-(void)viewDidLoad
{
 if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
{
    UIImage *image = [UIImage imageNamed:@"nav-bg-ipad.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}else{
    UIImage *image = [UIImage imageNamed:@"nav-bg-iphone.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
// do your other stuff now …

Hopefully this helps