导航栏中的button不会出现,因为我创buildCustomNavigationBar

我已经创build了一个自定义导航栏

#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 

比我在我的AppDelegate

  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]; 

我尝试在这样的页面中添加button

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

当我没有自定义导航栏此代码工作。

谢谢

拉卢卡

你必须有一个navigationItem来设置一个正确的barbutton,在我的testing(有效)我​​有:

  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];