【发布时间】:2014-02-05 05:01:17
【问题描述】:
如何在左侧barButton 或rightbarButton 旁边添加按钮,
我的意思是我需要在标题栏上有两个以上的按钮,这可能吗?
提前感谢
【问题讨论】:
标签: iphone objective-c
如何在左侧barButton 或rightbarButton 旁边添加按钮,
我的意思是我需要在标题栏上有两个以上的按钮,这可能吗?
提前感谢
【问题讨论】:
标签: iphone objective-c
您可以在 leftBarButton 中使用带有两个按钮的视图 -
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtnsView];
【讨论】:
将工具栏视为您的容器, 而不是 UIView 对象。和 因为 UIToolBar 是基于 UIView 的,所以它 可以添加到a的右侧 使用上述技巧的导航栏。这 以下代码显示了如何添加 右侧的两个标准按钮 侧面:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
同时查看 SO 帖子
【讨论】:
我建议使用UISegmentedControl 按住所有按钮并使用
[[UIButton alloc] initWithCustomView:] 显示段控制。
【讨论】:
以下是您如何进行此操作的示例。我在这个例子中使用了顶部导航栏附带的MasterDetailView 模板:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] init];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
leftButton.title = @"Left button";
rightButton.title = @"right button";
self.navigationItem.leftBarButtonItem = leftButton;
self.navigationItem.rightBarButtonItem = rightButton;
}
【讨论】:
你可以这样做,有一个叫做 UINavigationItem.rightBarButtonItems 的东西
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xxx.png"] style:UIBarButtonItemStyleDone target:self action:@selector(something)];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xxx.png"] 风格:UIBarButtonItemStyleDone 目标:自我 动作:@selector(something)];
self.navigationItem.rightBarButtonItems = @[settingsButton, menuButton];
【讨论】:
我认为你需要这样的东西
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:@"First" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:@"Second" style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationController.navigationItem.leftBarButtonItems=@[firstButton, secondButton];
它将在导航栏的左侧添加两个栏按钮项。你可以用同样的方法把它放在右边
self.navigationController.navigationItem.rightBarButtonItems=@[firstButton, secondButton];
【讨论】: