Iphone 是否以编程方式将UIBarButtonim添加到UIToolbar?
这是我需要的 我的应用程序有免费版和付费版。加载付费版本时,我需要在UIToolBar上设置3个UIBarButton。当免费版本加载时,我需要4个UIBarButtons。在最右边的按钮上,我需要蓝色,而其余的默认为黑色。我假设它们之间有灵活的空间来平衡它们。我试着通过IB来实现这一点,但我似乎无法让它以正确的间距工作。如您所见,带有3个按钮的底部工具栏与工具栏的间距不均匀。(我希望以编程方式执行此操作)Iphone 是否以编程方式将UIBarButtonim添加到UIToolbar?,iphone,xcode,uibarbuttonitem,uitoolbar,Iphone,Xcode,Uibarbuttonitem,Uitoolbar,这是我需要的 我的应用程序有免费版和付费版。加载付费版本时,我需要在UIToolBar上设置3个UIBarButton。当免费版本加载时,我需要4个UIBarButtons。在最右边的按钮上,我需要蓝色,而其余的默认为黑色。我假设它们之间有灵活的空间来平衡它们。我试着通过IB来实现这一点,但我似乎无法让它以正确的间距工作。如您所见,带有3个按钮的底部工具栏与工具栏的间距不均匀。(我希望以编程方式执行此操作) 首先在工具栏上添加购买按钮。结合并合成 if(AppPaid) { NSM
首先在工具栏上添加购买按钮。结合并合成
if(AppPaid) {
NSMutableArray *items = [YourToolBar.items mutableCopy];
[items removeObject:yourBuyButton];
YourToolBar.items = items;
[items release];
}
像这样创建4个项目。。。提供指向您的xib的链接,以便我们可以在您需要时隐藏所需的tabbarItem
。。。我希望这会有所帮助
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
尝试使用此选项可能会帮助您在默认情况下将“购买”按钮添加到工具栏,并使用一些唯一的值(例如-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
这是我在我的一个应用程序中已经使用的一些代码,在运行时用一个特定的按钮替换IB中标记为
-1
的项目,具体取决于显示的视图我可以用一些代码来提高清晰度吗按钮看起来很漂亮是的,我尝试了这些代码。我无法使“YourToolBar”没有错误。我还是个新手。我在上面添加了我的代码,所有东西都是通过IB实现的,所以IBOutlet是唯一的选择。你可以看到我有多少按钮。我想如果你能解释一下我如何“连接”你代码中的“你的工具栏”部分,我想这就是我的问题所在。好的,我能够让它显示出来,但是这会将我的视图向下推,并将工具栏放在顶部,我需要它放在视图的顶部和底部。好的,我通过更改您的一些代码,在底部显示了UIToolBar。但是按钮之间应该有一些灵活的空格,所以当我移除一个按钮时,按钮甚至在工具栏上。没有灵活的空格吗?我建议一件事,只是在每个按钮之间放置FlexibleSpace按钮项。如果BUYNow按钮的LITE_版本不应添加到BarButtonItems数组中,则为其设置条件。
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
#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