iphone 自定义导航栏按钮
通常使用的Navigation Item中有backBarButtonItem/leftBarButtonItem/rightBarButtonItem三个按键,其中backBarButtonItem为只读,不能修改,其他两个都可以自由修改.不但可以防止button,还可以用自定义的view来填充,比方说想在导航栏右侧防止两个按钮,
如图所示
代码如下:
1 UIView *rightBarView = [[UIView alloc]initWithFrame:CGRectMake(618, 0, 150, 44)]; 2 3 UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 4 5 [editBtn setFrame:CGRectMake(10, 10, 50, 25)]; 6 7 [editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside]; 8 9 [editBtn setTitle:@"编辑" forState:UIControlStateNormal]; [editBtn setTag:11]; 10 11 [rightBarView addSubview:editBtn]; 12 13 14 15 UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 16 17 [submitBtn setFrame:CGRectMake(80, 10, 70, 25)]; 18 19 [submitBtn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside]; 20 21 [submitBtn setTitle:@"提交订单" forState:UIControlStateNormal]; [submitBtn setTag:12]; 22 23 [rightBarView addSubview:submitBtn]; 24 25 UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBarView]; 26 27 self.navigationItem.rightBarButtonItem = rightBtn;