如何在左栏控制器或右栏控制器旁边添加一个按钮

时间:2011-05-13 06:27:27

标签: iphone objective-c

如何在左侧barButton旁边或rightbarButton旁边添加按钮, 我的意思是我需要在标题栏上有两个以上的按钮才有可能吗?

提前

thanx

6 个答案:

答案 0 :(得分:0)

摘自this blog post

  

将工具栏视为您的容器,   而不是UIView对象。和   因为UIToolBar是基于UIView的,所以   可以添加到右侧   导航栏使用上述技巧。该   以下代码显示了如何添加   右边有两个标准按钮   侧:

// 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帖子

Adding buttons to navigation bar

答案 1 :(得分:0)

您可以在leftBarButton中使用带有两个按钮的视图 -

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtnsView];

答案 2 :(得分:0)

我建议使用UISegmentedControl来保存所有按钮并使用 [[UIButton alloc] initWithCustomView:]显示细分受众群。

答案 3 :(得分:0)

以下是您如何进行此操作的示例。我使用的是此示例顶部导航栏附带的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;
}

答案 4 :(得分:0)

你可以这样做,有一种叫做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”]   style:UIBarButtonItemStyleDone target:self   动作:@selector(东西)];

     

self.navigationItem.rightBarButtonItems = @ [settingsButton,menuButton];

答案 5 :(得分:0)

我认为你需要这样的东西

 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];