xcode 栏按钮项以编程方式添加工具栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10444104/
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
bar button item add toolbar programmatically
提问by kEvin
I want to add button instead of bar button item into toolbar programmatically. I select it because button has image infodark defaut.
我想以编程方式将按钮而不是栏按钮项添加到工具栏中。我选择它是因为按钮具有图像 infodark defaut。
so how is this done programmatically?
那么这是如何以编程方式完成的呢?
回答by alloc_iNit
UIImage* image = [UIImage imageNamed:defaultImage];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setTitle:@"Display" forState:UIControlStateNormal & UIControlStateHighlighted];
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
[button addTarget:self action:@selector(button_Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
回答by Kevin
To add this button in UIToolbar
you need this code.
first you can initialize UIToolbar
after that
要在UIToolbar
您需要此代码中添加此按钮。首先你可以UIToolbar
在那之后初始化
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];
[buttons addObject: barButtonItem];
[buttons addObject: btnFav];
[buttons addObject: btnGeneralPopOverController];
[buttons addObject: showInfo];
[mytoolbar setItems:buttons animated:NO];