UINavigationItem: Add one more button

2019-07-02 08:00发布

enter image description here

How can I add new button in navigationItem? Say I need to add it near right button.

Code used for left right.

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

How can I add one more button?

5条回答
神经病院院长
2楼-- · 2019-07-02 08:38

Try this. It worked for me.

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];
查看更多
一夜七次
3楼-- · 2019-07-02 08:40

you can add more buttons

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

same for right buttons

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];
查看更多
做自己的国王
4楼-- · 2019-07-02 08:55

Middle Button

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;

Right Bar Button

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

Left Bar button

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]

Output

enter image description here

查看更多
冷血范
5楼-- · 2019-07-02 08:56

There are Different Approach to add more then one Buttons on right side of navigationcontroller.one of the Simplist way is given below

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

Hope it will be helpful for you.Thanks

查看更多
混吃等死
6楼-- · 2019-07-02 08:58
NSArray *tempArray2= [[NSArray alloc] initWithObjects:self.shareBtn,self.gameCenterBtn,nil];
self.navigationItem.leftBarButtonItems=tempArray2;
查看更多
登录 后发表回答