Buttons from navigation bar don t appear since I c

2019-09-14 16:27发布

问题:

I have created a custom navigation Bar

#import "UICustomNavigationBar.h"


@implementation UICustomNavigationBar 
 - (void)drawRect:(CGRect)rect {
CGContextRef context=UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, self.bounds , [UIImage    imageNamed:@"capitonhaut.png"].CGImage);

if(UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
{
    CGContextDrawImage(context, CGRectMake(155, 5, 170, 37), [UIImage imageNamed:@"logo_BEST.png"].CGImage);
}
else {
    CGContextDrawImage(context, CGRectMake(75, 5, 170, 37), [UIImage imageNamed:@"logo_BEST.png"].CGImage);
}
}

@end

Than in my AppDelegate I have

 UICustomNavigationBar *navBar = [[UICustomNavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width,50)];

[self.window sendSubviewToBack:self.tabBar.view];
[self.window addSubview:navBar];

[navBar release];

I try to add buttons in some pages like this

[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc]   initWithTitle:NSLocalizedString(@"Ok",@"Ok") style:UIBarButtonItemStyleBordered target:self action:@selector(onSortButtonTap:)]];

When I din not have custom navigation bar this code worked.

Thanks

Raluca

回答1:

You must have a navigationItem to set a right bar button, in my test (that works) I have:

    UICustomNavigationBar *navBar =[[UICustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    [self.view addSubview:navBar];           
    [navBar pushNavigationItem:[[[UINavigationItem alloc] initWithTitle:nil] autorelease] animated:NO];
    navBar.topItem.rightBarButtonItem = [[[UIBarButtonItem alloc]   initWithTitle:NSLocalizedString(@"Ok",@"Ok") style:UIBarButtonItemStyleBordered target:self action:@selector(onSortButtonTap:)] autorelease];

    [navBar release];