我想显示两个按钮,或者可以使用相同的TouchUpInSide方法在导航栏的左侧说两个图像。当我单击这两个按钮时,最左边的按钮应向左变换更多;当我再次单击这两个按钮时,最左边的按钮又回到原始位置。或另一种方式:我可以在导航栏的左侧放置两个UIImageView,并放置一个具有清晰背景的按钮,并在其上提供事件。
那么有可能吗?如何?
如果只想添加简单的UIButton,则可以使用以下方法。
1)创建多个UIButton对象
2)将所有这些按钮添加到UIView对象
3)创建UIBarButtonItem并将UIView作为自定义视图传递
代码如下:
// create the container view
UIView* viewContainer = [[UIView alloc] init];
// Create buttons and add it to the container view
UIButton* btnAdd = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnAdd setTitle:@"Add" forState:UIControlStateNormal];
[viewContainer addSubview:btnAdd];
UIButton* btnRefresh = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnRefresh setTitle:@"Refresh" forState:UIControlStateNormal];
[viewContainer addSubview:btnRefresh];
// now create a Bar button item with custom view
UIBarButtonItem* barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:viewContainer];
// Set the navigation bar's right button item
self.navigationItem.rightBarButtonItem = barBtnItem;
否则,如果您想添加BarUIBarButtonItems,则可以添加多个barbutton,如Anbu.Karthik所说。
这是添加多个条形按钮项目的代码。
// Create UIToolbar to add two buttons in the right
UIToolbar* toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// Create standard "add" button
UIBarButtonItem* btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
btnAdd.style = UIBarButtonItemStyleBordered;
// Create spacer
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// create standard "refresh" button
UIBarButtonItem* btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(btnRefresh_Clicked:)];
btnRefresh.style = UIBarButtonItemStyleBordered;
// stick the buttons in the UIToolbar
[toolBar setItems:@[btnAdd, spacer, btnRefresh] animated:NO];
// Put the toolbar in the UINavigationBar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];
希望这会帮助你。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句