xcode 以编程方式将 UIBarButtonItem 添加到 UIToolbar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11044665/
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
Add UIBarButtonItem to UIToolbar programmatically?
提问by Jason
Here's what I need.
这就是我需要的。
I have a free version and a paid version of my app. When the paid version loads, I need 3 UIBarButtons on my UIToolBar. When the Free version loads I need 4 UIBarButtons. On the far right barButton, I need the tint Blue, while the rest are default black. And I'm assuming the flexible space between them to even them out. I've tried doing this through IB, but I can't seem to get it to work with the spacing correct. As you can see, the bottom toolbar with 3 buttons are not spaced evenly with the toolbar. (I would like to do this programmatically)
我的应用程序有免费版本和付费版本。当付费版本加载时,我的 UIToolBar 上需要 3 个 UIBarButtons。当免费版本加载时,我需要 4 个 UIBarButton。在最右边的 barButton 上,我需要蓝色,而其余的是默认黑色。我假设它们之间有灵活的空间来平衡它们。我已经尝试通过 IB 这样做,但我似乎无法让它以正确的间距工作。如您所见,带有 3 个按钮的底部工具栏与工具栏的间距不均匀。(我想以编程方式执行此操作)
#ifdef LITE_VERSION
#else
[buyButton removeFromSuperview];
#endif
回答by Abhishek
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 320, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* BarbuttonsArray = [[NSMutableArray alloc] initWithCapacity:5];
// create a clearAll button
UIBarButtonItem * clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear All"
style:UIBarButtonItemStylePlain
target:self
action:@selector(clearAllAction:)];
clearAllButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray addObject:clearAllButton];
[clearAllButton release];
// create a calculate button
UIBarButtonItem *calculateButton = [[UIBarButtonItem alloc]initWithTitle:@"Calculate"
style:UIBarButtonItemStylePlain
target:self
action:@selector(calculateButton:)];
calculateButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray addObject:calculateButton];
[calculateButton release];
// create a settingButton
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc]initWithTitle:@"Setting"
style:UIBarButtonItemStylePlain
target:self
action:@selector(settingButton:)];
settingButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray addObject:settingButton];
[settingButton release];
// create a buyNowButton
UIBarButtonItem *buyNowButton = [[UIBarButtonItem alloc]initWithTitle:@"Buy Now"
style:UIBarButtonItemStylePlain
target:self
action:@selector(buyNowButton:)];
buyNowButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray addObject:buyNowButton];
[buyNowButton release];
// put the BarbuttonsArray in the toolbar and release them
[toolbar setItems:BarbuttonsArray animated:NO];
[BarbuttonsArray release];
// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
//before using these lines of code you have to alloc and make the property of UINavigationController in your appDelegate
Try to use this may help you
尝试使用这可能对您有所帮助
回答by Mani
First add buy button on your toolbar. bind and synthesize.
首先在工具栏上添加购买按钮。绑定和合成。
if(AppPaid) {
NSMutableArray *items = [YourToolBar.items mutableCopy];
[items removeObject:yourBuyButton];
YourToolBar.items = items;
[items release];
}
回答by ganesh manoj
IBOutlet UITabBarItem * item1;
like this create 4 items... give link to your xib so that we can hide required tabbarItem
when ever you want... I hope this will help
像这样创建 4 个项目...提供指向您的 xib 的链接,以便我们可以tabbarItem
在您需要时隐藏所需...我希望这会有所帮助
回答by Dan F
Add the buy button to your toolbar by default, and tag it with some unique value (say -1), and then at runtime you can remove it from the toolbar like so:
默认情况下将购买按钮添加到您的工具栏,并用一些唯一值(比如 -1)标记它,然后在运行时您可以像这样从工具栏中删除它:
#ifdef LITE_VERSION
//Dont need to do anything, view is set up as lite version by default
#else
NSArray *elements = toolBar.items;
NSMutableArray *newElements = [[NSMutableArray alloc] initWithCapacity:[elements count]];
for ( UIBarItem *item in elements )
{
if ( item.tag != -1 )
{
//Item is not the buy button, add it back to the array
[newElements addObject:item];
}
}
[toolBar setItems:newElements];
#endif
This is some code I use in one of my apps already to replace an item that is tagged in IB with -1
with a specific button at runtime, depending on which view is on display
这是我在我的一个应用程序中使用的一些代码,用于在-1
运行时用特定按钮替换在 IB 中标记的项目,具体取决于显示的视图