美文网首页OC开发资料收集区
ios 给导航栏添加button

ios 给导航栏添加button

作者: oc123 | 来源:发表于2017-05-19 10:55 被阅读247次

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

    本文链接:https://www.haomeiwen.com/subject/lgfyxxtx.html