3

在此处输入图像描述

如何在导航项中添加新按钮?假设我需要在右侧按钮附近添加它。

用于左右的代码。

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_cancelButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_shareButton];

如何再添加一个按钮?

4

5 回答 5

4

您可以添加更多按钮

self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];

右键相同

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];
于 2013-05-08T12:14:36.307 回答
4

有不同的方法可以在导航控制器的右侧添加一个以上的按钮。下面给出了一种最简单的方法

 UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];

UIButton *exampleButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
exampleButton1.frame = CGRectMake(0, 2, 30, 40);
[exampleButton1 addTarget:self action:@selector(FirstMethod) forControlEvents:UIControlEventTouchUpInside];
[exampleButton1 setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
[buttonView addSubview:exampleButton1];


UIButton *exampleButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
exampleButton2.frame = CGRectMake(70, 2, 30, 40);
[exampleButton2 addTarget:self action:@selector(Second method) forControlEvents:UIControlEventTouchUpInside];
[exampleButton2 setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];

[buttonView addSubview:exampleButton2];

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];

希望对你有帮助。谢谢

于 2013-05-08T13:28:18.410 回答
2

中间按钮

UIButton *btnShow = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 34, 34)];
    btnShow.backgroundColor = [UIColor colorWithWhite:0.411 alpha:1.000];
    [btnShow setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [btnShow setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
    self.navigationItem.titleView = btnShow;

右栏按钮

 UIButton *tempButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempButton1 setFrame:CGRectMake(0, 0, 34, 34)];
    tempButton1.tag = 0;
    [tempButton1 addTarget:self action:@selector(btnBackClicked:) forControlEvents:UIControlEventTouchUpInside];

    [tempButton1 setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [tempButton1 setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:tempButton1]];

左栏按钮

UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton setFrame:CGRectMake(0, 0, 34, 34)];
[tempButton addTarget:self action:@selector(btnBackClicked:) forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[tempButton setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:tempButton]

输出

在此处输入图像描述

于 2013-05-08T12:21:18.570 回答
1
NSArray *tempArray2= [[NSArray alloc] initWithObjects:self.shareBtn,self.gameCenterBtn,nil];
self.navigationItem.leftBarButtonItems=tempArray2;
于 2013-05-08T12:08:46.927 回答
1

试试这个。它对我有用。

UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    flex.width = 20;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0, 0, 40, 30)];
    [btn setImage:[UIImage imageNamed:@"mac-gray.png"] forState:UIControlStateNormal];

    UIBarButtonItem *bar1 = [[UIBarButtonItem alloc]initWithCustomView:btn];

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setFrame:CGRectMake(0, 0, 40, 30)];
    [btn2 setImage:[UIImage imageNamed:@"mac-gray.png"] forState:UIControlStateNormal];

    UIBarButtonItem *bar2 = [[UIBarButtonItem alloc]initWithCustomView:btn2];

    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:bar1,flex,bar2, nil];
于 2013-05-08T12:12:07.047 回答