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

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

UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];

    [tools setTintColor:[self.navigationController.navigationBar tintColor]];

    [tools setAlpha:[self.navigationController.navigationBar alpha]];

    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
    

    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickSettings:)];

    UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickEdit:)];

    anotherButton1.tintColor = [UIColor redColor];
    
    [buttons addObject:anotherButton];

    [buttons addObject:anotherButton1];
    
    [tools setItems:buttons animated:NO];
    
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithCustomView:tools];
    
    self.navigationItem.rightBarButtonItem = myButton;

- (void)clickSettings:(UIBarButtonItem *)clickSettings
{
    NSLog(@"我是添加按钮");
}
- (void)clickEdit:(UIBarButtonItem *)clickEdit
{
    NSLog(@"我是编辑按钮");
}




你可能感兴趣的:(iOS,开发常用方法)