xcode 向工具栏添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8606910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding buttons to toolbar
提问by sharon
I need to add a transparent toolbar and add 4 buttons with images on it. How can i do this by code?
我需要添加一个透明的工具栏并添加 4 个带有图像的按钮。我怎样才能通过代码做到这一点?
My workings so far;
到目前为止我的工作;
i have added the toolbar and made it transparent
and now i am trying to add 4 buttons with images. How can i do this ? (These 4 buttons should also have a action
method so when a person clicks on it an action should fire up)
我已经添加了工具栏并使其透明,现在我正在尝试添加 4 个带有图像的按钮。我怎样才能做到这一点 ?(这 4 个按钮也应该有一个action
方法,所以当一个人点击它时,一个动作应该被触发)
toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 320 , 320 , 60)];
[toolBar1 setBarStyle:UIBarStyleBlack];
[toolBar1 setTranslucent:YES];
NSMutableArray* allthebuttons = [[NSMutableArray alloc] initWithCapacity:4];
UIBarButtonItem *buttonWithImage = [[UIBarButtonItem alloc] ...... // Now what ??
[self.view addSubview:toolBar1];
回答by Saurabh
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(action];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"Item1" style:UIBarButtonItemStyleBordered target:self action:@selector(action];
NSArray *buttons = [NSArray arrayWithObjects: item1, item2, nil];
[toolBar setItems: buttons animated:NO];
[item1 release];
[item2 release];
Just make above code for 4 buttons
只需为 4 个按钮制作上面的代码
update 1 :
更新1:
use following code to get images in buttons
使用以下代码获取按钮中的图像
UIImageView *btn1Img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Btn1Img"]];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1Img];
回答by ader
NSMutableArray *buttonsArray = [[NSMutableArray alloc] init];
UIBarButtonItem *myButton1=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"email.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed1:)];
[buttonsArray addObject:myButton1];
UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed2:)];
[buttonsArray addObject:myButton2];
[self setToolbarItems:buttonsArray animated:YES];
Note, for the above you'll want to use the toolbar on the navigation view controller.
请注意,对于上述情况,您需要使用导航视图控制器上的工具栏。