我是 iPhone 开发者的新手,
我想在导航栏上的后退按钮旁边再添加一个按钮,我正在使用导航控制器,所以后退按钮已经在左侧。在最右侧,我添加了一个按钮
self.navigationItem.rightBarButtonItem = ModeButton;
但是当我写的时候,
self.navigationItem.leftBarButtonItem = NewButton;
NewButton 覆盖我的后退按钮,我想将我的新按钮放在后退按钮旁边。
任何帮助将不胜感激。
我是 iPhone 开发者的新手,
我想在导航栏上的后退按钮旁边再添加一个按钮,我正在使用导航控制器,所以后退按钮已经在左侧。在最右侧,我添加了一个按钮
self.navigationItem.rightBarButtonItem = ModeButton;
但是当我写的时候,
self.navigationItem.leftBarButtonItem = NewButton;
NewButton 覆盖我的后退按钮,我想将我的新按钮放在后退按钮旁边。
任何帮助将不胜感激。
UINavigationItem
有一个属性leftItemsSupplementBackButton
,你可以使用它。
leftItemsSupplementBackButton 一个布尔值,指示除了后退按钮之外是否显示左侧项目。
@property BOOL leftItemsSupplementBackButton
讨论 通常,自定义左栏按钮项目的存在会导致后退按钮被移除以支持自定义项目。将此属性设置为 YES 会导致 leftBarButtonItems 或 leftBarButtonItem 属性中的项目显示在后退按钮的右侧——也就是说,它们显示在后退按钮的旁边,而不是代替后退按钮。当设置为 NO 时,将显示这些属性中的项目而不是后退按钮。此属性的默认值为 NO。
UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
我认为你应该有一个灵活的空间让它不与其他重叠,也许这样的东西应该让你开始。
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(Back:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"newButton" style:UIBarButtonItemStyleBordered target:self action:@selector(newButtonMethod:)];
NSArray *navBarItems = [[NSArray alloc] initWithObjects:back, flexibleSpace, newButton, nil];
编辑:
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(Back:)];
[buttons addObject:back];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[buttons addObject:flexibleSpace];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"newButton" style:UIBarButtonItemStyleBordered target:self action:@selector(newButtonMethod:)];
[buttons addObject:newButton];
UIToolbar* myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];
[myToolbar setTintColor:[self.navigationController.navigationBar tintColor]];
[myToolbar setItems:buttons animated:NO];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithCustomView:myToolbar];
self.navigationItem.leftBarButtonItem = myButton;
创建一个包含两个按钮的自定义视图,然后按如下方式创建条形按钮项:
UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:userCustomView];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
编辑:下面的代码只是如何创建两个按钮的示例,
UIView *customView = [[UIView alloc] init];
customView.frame = CGRectMake(0, 0, 100, 40);
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"Add" forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 0, 50, 40);
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"del" forState:UIControlStateNormal];
button2.frame = CGRectMake(60, 0, 50, 40);
[customView addSubview:button2];
[customView addSubview:button1];
UIBarButtonItem *barItem =[[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.leftBarButtonItem = barItem;
为此,您必须制作一个 Custom UIBarButtonItem
。答案是,您必须分别放置两个按钮,BackButton(用户定义)和 NewButton,如下所示:
UIView *viewForLeftBarButton = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 100, 35)];
[viewForLeftBarButton addSubview:BackButton];
[viewForLeftBarButton addSubview:NewButton];
UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:viewForLeftBarButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;