如何将几个 UIBarButtonItem 添加到导航栏?

Posted

技术标签:

【中文标题】如何将几个 UIBarButtonItem 添加到导航栏?【英文标题】:How to add several UIBarButtonItems to a NavigationBar? 【发布时间】:2011-12-08 13:05:49 【问题描述】:

我想在UINavigationBar 上绘制多个按钮。这些将在右侧或左侧。

【问题讨论】:

【参考方案1】:

我做了一个示例,其中我在 NaviagationBar 的右侧有两个按钮(即编辑和 +)。

1) 您必须创建一个NSMutableArray(即示例中的“按钮”)并将UIBarButtonItem(即示例中的bi1 和bi2)添加到NSMutableArray(即按钮)中。

2) 将NSMutableArray(即示例中的按钮)添加到工具栏(即示例中的UIToolbar *tools)。

3) 将工具栏添加到 NavigationBar。

 NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
 UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add bar button1.

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
//[bi1 release]; Do not release if ARC enabled.

// Add bar button2.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
//[bi2 release]; Do not release if ARC enabled.

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
//[buttons release];  Do not release if ARC enabled.

 // Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
//[twoButtons release]; Do not release if ARC enabled.

【讨论】:

【参考方案2】:

在您的 xib 文件中执行此操作,并在标题中创建属性或只是变量

@property (nonatomic, retain) IBOutlet UIBarButtonItem *itemOne;

然后在xib中连接它。享受

【讨论】:

【参考方案3】:

在代码中创建一个新的 UIToolbar 并将您的按钮添加到工具栏。然后将 self.navigationItem.rightBarButton 设置为您新创建的工具栏(注意该示例没有 ARC,因此您可能需要删除对 release 的调用):

    // create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 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:@selector(addRow)];
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];

[buttons addObject:self.editButtonItem];

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

【讨论】:

【参考方案4】:

在这里,我为您提供了用于按钮和标签的示例代码。您可以创建按钮而不是标签和图像我创建的。希望对你有帮助

- (void) setLabelForPotraite 

    bar = [self.navigationController navigationBar];    
    [bar setBackgroundColor:[UIColor clearColor]];
    barImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navImg.png"]];
    [bar addSubview:barImg];


    tick_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(86, 6,34, 33)];
    tick_img_lbl.image=[UIImage imageNamed:@"tick-1.png"];
    [bar addSubview:tick_img_lbl];
    [tick_img_lbl release];

    tickCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(126, 2, 50, 40)];
    tickCount_lbl.text=@"";
    tickCount_lbl.font=[UIFont fontWithName:@"Arial" size:24.0];
    [tickCount_lbl setTextAlignment:UITextAlignmentCenter];
    tickCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
    tickCount_lbl.textColor=[UIColor whiteColor];
    tickCount_lbl.backgroundColor=[UIColor clearColor];
    [bar addSubview:tickCount_lbl];
    [tickCount_lbl release];

    cross_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(181, 6, 34, 33)];
    cross_img_lbl.image=[UIImage imageNamed:@"x_green.png"];

    [bar addSubview:cross_img_lbl];
    [cross_img_lbl release];

    crossCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(221, 2, 50, 40)];
    crossCount_lbl.text=@"";
    crossCount_lbl.font=[UIFont fontWithName:@"Arial" size:24.0];
    crossCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
    crossCount_lbl.textColor=[UIColor whiteColor];
    [crossCount_lbl setTextAlignment:UITextAlignmentCenter];
    crossCount_lbl.backgroundColor=[UIColor clearColor];
    [bar addSubview:crossCount_lbl];
    [crossCount_lbl release];

    master_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(269, 6, 34, 33)];
    master_img_lbl.image=[UIImage imageNamed:@"thumb.png"];
    [bar addSubview:master_img_lbl];
    [master_img_lbl release];

如果有任何理解或其他方面的问题,请告诉我。

【讨论】:

以上是关于如何将几个 UIBarButtonItem 添加到导航栏?的主要内容,如果未能解决你的问题,请参考以下文章

如何在所有视图控制器中将自定义 uibarbuttonItem 添加到我的导航项?

如何以编程方式添加 UIBarButtonItem?

如何以编程方式将 UIBarButtonItem 添加到拖到视图上的 UINavigationBar

如何在 UINavigationController 中为所有视图控制器添加 UiBarButtonItem

如何将几个 EXTJS Grids 数据写入一个请求?

将几个项目的背景颜色复制到剪贴板[重复]