【问题标题】:Add bar button in UINavigationbar在 UINavigationbar 中添加栏按钮
【发布时间】:2012-07-04 09:00:05
【问题描述】:

我是 iPhone 开发者的新手,

我想在导航栏上的后退按钮旁边再添加一个按钮,我正在使用导航控制器,所以后退按钮已经在左侧。在最右侧,我使用

添加了一个按钮

self.navigationItem.rightBarButtonItem = ModeButton;

但是当我写的时候,

self.navigationItem.leftBarButtonItem = NewButton;

NewButton 覆盖了我的后退按钮,我想将我的新按钮放在后退按钮旁边。

我们将不胜感激。

【问题讨论】:

  • 我认为链接here可以帮助你。
  • 我的解决方案对您有用吗?

标签: iphone ipad uinavigationbar uibarbuttonitem back-button


【解决方案1】:

UINavigationItem 有一个属性leftItemsSupplementBackButton,你可以使用它。

leftItemsSupplementBackButton 一个布尔值,指示除了后退按钮之外是否显示左侧项目。

@property BOOL leftItemsSupplementBackButton

讨论 通常,自定义左栏按钮项目的存在会导致移除后退按钮以支持自定义项目。将此属性设置为 YES 会导致 leftBarButtonItems 或 leftBarButtonItem 属性中的项目显示在后退按钮的右侧——也就是说,它们显示在后退按钮的旁边,而不是代替后退按钮。当设置为 NO 时,将显示这些属性中的项目而不是后退按钮。此属性的默认值为 NO。

来自UINavigationController Class Reference。

UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;

【讨论】:

  • 你能推荐一些例子吗,我在这里很新。
  • 如果你设置了布尔值,后退按钮就会出现,并且你的新按钮会直接显示在后退按钮的旁边。我的答案是通过包含代码来编辑的。
【解决方案2】:

我认为你应该有一个灵活的空间让它不与其他空间重叠,也许这样的东西应该让你开始。

    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;

【讨论】:

  • 如何将此数组添加到导航栏?
  • 我得到了 SIGABRT 不兼容的指针类型从 NSArray 到 UIBarButtonItem
  • 大声笑,你的很好,但我接受的答案是简短而标准的。但我会投票赞成你的回答。
【解决方案3】:

创建一个包含两个按钮的自定义视图,然后按如下方式创建条形按钮项:

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;

【讨论】:

  • 你能推荐一些例子吗,我在这里很新。我想在左侧而不是右侧添加按钮
  • 您的代码是正确的,但问题是,我不想添加返回按钮,它已经添加到导航栏中我不知道他的名字。
【解决方案4】:

为了实现这一点,您必须创建一个自定义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;

【讨论】:

  • 我没有创建返回按钮,它是自动创建的,所以这里出现错误,[viewForLeftBarButton addSubview:BackButton]; 它显示 BackButton 是意外标识符。
猜你喜欢
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多