ios 给导航栏添加button

有些时候,我们要给导航栏添加button,本文简单介绍一下如何添加,代码如下:

self.navigationItem.rightBarButtonItem = [UIBarButtonItem navItemWithTitle:@"按钮Title" target:self action:@selector(buttonClick)];//注意 - 该初始方法是自定义方法

下面是为UIBarButtonItem添加的分类中的一个方法

//想要修改添加的button的样式,在如下代码中进行相应的改动
+(instancetype)navItemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *moreBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setFrame:CGRectMake(0,5,70,50)];
    [moreBtn setTitle:title forState:UIControlStateNormal];
    [moreBtn setTitleColor:ColorLightGray forState:UIControlStateNormal];
    [moreBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [moreBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [moreBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[self alloc]initWithCustomView:moreBtn];
}

更复杂的样式设置,本文就不多做介绍了,请自行摸索;

你可能感兴趣的:(ios 给导航栏添加button)