这就是我需要的。
我的应用程序有免费版本和付费版本。当付费版本加载时,我的 UIToolBar 上需要 3 个 UIBarButton。当免费版本加载时,我需要 4 个 UIBarButtons。在最右侧的栏按钮上,我需要蓝色,而其余的则默认为黑色。我假设它们之间有灵活的空间以使它们平衡。我已经尝试通过 IB 执行此操作,但我似乎无法使其在正确的间距下工作。正如您所看到的,带有 3 个按钮的底部工具栏与工具栏的间距不均匀。 (我想以编程方式执行此操作)
#ifdef LITE_VERSION
#else
[buyButton removeFromSuperview];
#endif
最佳答案
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
尝试使用这个可能会对您有所帮助
关于iphone - 以编程方式将 UIBarButtonItem 添加到 UIToolbar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044665/