Addimg mutiple button on Navigation bar in Iphone SDK

冷暖自知 提交于 2019-12-01 12:26:50

问题


I want to add two buttons with custom image to Navigation Bar with some specific position.

I found solution But it is for Right/Left Navigation Bar Button.

My code for that is:

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

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

// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];

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

How can i do this?


回答1:


UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[vieww addSubview:yourBtn1];
[vieww addSubview:yourBtn2];

[self.navigationController.navigationBar addSubview:vieww];    

And if you want to remove yourButtonView then make is global object;

in .h

UIView *vieww;

and in .m

-(void)viewWillDisappear:(BOOL)animated
{
    [vieww removeFromSuperview];
}

Or follow this for more Link




回答2:


if you are using >iOS 5, then use this.

UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:@" + " style:UIBarButtonItemStyleDone target:self action:@selector(action1:)];
    UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:@" - " style:UIBarButtonItemStyleDone target:self action:@selector(action2:) ];
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];

for < iOS 5 u can use following:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
        tools.barStyle = UIBarStyleBlackOpaque;
        // create the array to hold the buttons, which then gets added to the toolbar
        NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
        [buttons addObject:btn1];
        [buttons addObject:btn2];
        [tools setItems:buttons animated:NO];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];



回答3:


Instead of adding toolbar, you can create one UIView, add two buttons on that view.

    UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];



回答4:


If you want to do this in combination with using a storyboard, take a look at this question.



来源:https://stackoverflow.com/questions/13890380/addimg-mutiple-button-on-navigation-bar-in-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!