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

来源:互联网 发布:上海大承网络 空中网 编辑:程序博客网 时间:2024/05/21 17:01

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

<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>




0 0
原创粉丝点击