IOS-9-UIBarButtonItem导航条-添加自定义Left或者Right按钮

1.用系统自带的返回按钮

 

UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]

                                          initWithTitle:@"返回"

                                          style:UIBarButtonSystemItemCancel

                                          target:self

                                          action:@selector(doClickBackAction:)];

    self.navigationItem.backBarButtonItem = leftBarButtonItem;


2.自己定义导航条右上方按钮
@property (nonatomic,strong)UIButton *issueButton;

   //自定义一个导航条右上角的一个button

   UIImage *issueImage = [UIImageimageNamed:@"btnpressed.png"];

   

self.issueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.issueButton.frame = CGRectMake(0, 0, 54, 30);

    [_issueButton setBackgroundImage:issueImage forState:UIControlStateNormal];

    [_issueButton setTitle:@"发布" forState:UIControlStateNormal];

    _issueButton.titleLabel.font = [UIFont systemFontOfSize:13];

    [_issueButton addTarget:self action:@selector(issueBton) forControlEvents:UIControlEventTouchUpInside];

    [issueButtonaddTarget:selfaction:@selector(doClickBackAction:)forControlEvents:UIControlEventTouchUpInside];

   //添加到导航条

   UIBarButtonItem *leftBarButtomItem = [[UIBarButtonItemalloc]initWithCustomView:issueButton];

   self.navigationItem.rightBarButtonItem = leftBarButtomItem;


感谢大家指出的不妥之处,再次特别感谢!

你可能感兴趣的:(iOS)