简体   繁体   English

Iphone SDK导航栏中的Addimg多个按钮

[英]Addimg mutiple button on Navigation bar in Iphone SDK

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? 我怎样才能做到这一点?

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; 如果要删除yourButtonView,则make是全局对象;

in .h

UIView *vieww;

and in .m 并在.m

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

Or follow this for more Link 或关注此链接以获取更多链接

if you are using >iOS 5, then use this. 如果您使用的是> iOS 5,请使用此功能。

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: 对于<iOS 5,您可以使用以下代码:

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

除了添加工具栏,您还可以创建一个UIView,在该视图上添加两个按钮。

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

如果要结合使用情节提要板来执行此操作,请查看此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM