buttons on navigation bar not loading

倾然丶 夕夏残阳落幕 提交于 2019-12-06 16:19:28

问题


i want to add buttons on navigation bar but its not loading on bar.Please help.Following is my code.please tell me where i am going wrong.The view i am using is landscape

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

回答1:


try this

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


来源:https://stackoverflow.com/questions/6464920/buttons-on-navigation-bar-not-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!