iOS UIBarButtonItem CustomView

时间:2016-04-19 03:50:13

标签: ios uinavigationcontroller uibarbuttonitem

我将自定义视图设为leftBarButtonItem,但它没有左对齐,我在调试器上找到UInavigationBarBackIndicatorView但不可见,我想左对齐UInavigationBarBackIndicatorView

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
view.backgroundColor = [UIColor redColor];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.leftBarButtonItem = leftItem;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close"
                                                                          style:UIBarButtonItemStylePlain
                                                                         target:self
                                                                         action:@selector(cancel)];

Debuger View:

I wish

1 个答案:

答案 0 :(得分:0)

我希望这可以帮助你实现你想要的目标。

UIButton *aBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
aBtn.backgroundColor = [UIColor redColor];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer.width = -13.0;
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:spacer, aBtn, nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close"
                                                                      style:UIBarButtonItemStylePlain
                                                                     target:self
                                                                     action:@selector(cancel)];

不要忘记向按钮添加动作。