iOS 开发中在导航栏添加多个按钮并改变它的位置

跳过普通的设置左右按钮我们直接讲解给导航栏设置多个按钮 实现代码如下

<span style="font-size:18px;">UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];</span>
<span style="font-size:18px;">
    [tools setTintColor:[self.navigationController.navigationBar tintColor]];</span>
<span style="font-size:18px;">
    [tools setAlpha:[self.navigationController.navigationBar alpha]];</span>
<span style="font-size:18px;">
    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
    </span>
<span style="font-size:18px;">
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickSettings:)];</span>
<span style="font-size:18px;">
    UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickEdit:)];</span>
<span style="font-size:18px;">
    anotherButton1.tintColor = [UIColor redColor];
    
    [buttons addObject:anotherButton];</span>
<span style="font-size:18px;">
    [buttons addObject:anotherButton1];
    
    [tools setItems:buttons animated:NO];
    
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithCustomView:tools];
    
    self.navigationItem.rightBarButtonItem = myButton;
</span>

<span style="font-size:18px;">- (void)clickSettings:(UIBarButtonItem *)clickSettings
{
    NSLog(@"我是添加按钮");
}</span>
<span style="font-size:18px;">- (void)clickEdit:(UIBarButtonItem *)clickEdit
{
    NSLog(@"我是编辑按钮");
}
</span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值