如何在navigationItem中添加新按钮?假设我需要在右键附近添加它。
用于左侧的代码。
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_cancelButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_shareButton];
如何再添加一个按钮?
答案 0 :(得分:4)
您可以添加更多按钮
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];
右键按钮
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];
答案 1 :(得分:4)
有一种不同的方法可以在navigationcontroller的右侧添加多个按钮。简单方法之一如下所示
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];
希望它对你有所帮助。谢谢
答案 2 :(得分: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]
<强>输出强>
答案 3 :(得分:1)
NSArray *tempArray2= [[NSArray alloc] initWithObjects:self.shareBtn,self.gameCenterBtn,nil];
self.navigationItem.leftBarButtonItems=tempArray2;
答案 4 :(得分: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];