OC开发资料收集区

ios 给导航栏添加button

2017-05-19  本文已影响247人  oc123

有些时候,我们要给导航栏添加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];
}

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

上一篇下一篇

猜你喜欢

热点阅读