导航栏上的按钮不加载

2019年3月11日 37点热度 0条评论 来源: Dipti Y W

我想在导航栏上添加按钮,但它不会加载在bar.Please help.Following是我的code.please告诉我我要去哪里错了。我使用的视图是风景导航栏上的按钮不加载

- (void)viewDidLoad { 
    [super viewDidLoad];  
    CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0); 
    navBar = [[UINavigationBar alloc] initWithFrame:rect]; 
    navBar.items = self.navigationController.navigationBar.items; 
    navBar.delegate = self; 
    //[navBar release]; 
    navBar.tintColor =[UIColor blackColor]  

    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Photo"  style:UIBarButtonItemStyleBordered target:self action:@selector(takePhoto)]; 

    UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:@"Camera List" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)]; 

    self.navigationItem.rightBarButtonItem = rightButton; 
    self.navigationItem.leftBarButtonItem = backButton; 

    //navBar.topItem.rightBarButtonItem = rightButton; 
// navBar.topItem.leftBarButtonItem = backButton; 

    [self.view addSubview:navBar]; 

    [backButton release]; 
} 


===========解决方案如下:

试试这个

(void)viewDidLoad { 
    [super viewDidLoad]; 
    CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0); 
    navBar = [[UINavigationBar alloc] initWithFrame:rect]; 
    navBar.items = self.navigationController.navigationBar.items; 
    navBar.delegate = self; 

    UINavigationItem *navigationItem = [UINavigationItem alloc]; 
    [navBar pushNavigationItem:navigationItem animated:NO]; 
    [navigationItem release]; 

    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Photo" style:UIBarButtonItemStyleBordered target:self action:@selector(takePhoto)]; 

    UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:@"Camera List" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)]; 

    self.navigationItem.rightBarButtonItem = rightButton; 
    self.navigationItem.leftBarButtonItem = backButton; 

    [self.view addSubview:navBar]; 

    [backButton release]; 
    [rightButton release]; 
} 
    原文作者:Dipti Y W
    原文地址: https://stackoverflow.com/q/6464920
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。