How to add a button to UINavigationBar?

后端 未结 7 1775
暖寄归人
暖寄归人 2020-12-02 06:19

How to add a button to UINavigationBar programmatically?

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 06:57

    Adding custom button to navigation bar ( with image for buttonItem and specifying action method (void)openView{} and).

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 32, 32);
    [button setImage:[UIImage imageNamed:@"settings_b.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
    [barButton setCustomView:button];
    self.navigationItem.rightBarButtonItem=barButton;
    
    [button release];
    [barButton release];
    

提交回复
热议问题