您的位置:首页 > 移动开发 > IOS开发

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

2016-01-11 11:11 531 查看
跳过普通的设置左右按钮我们直接讲解给导航栏设置多个按钮 实现代码如下

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息