iphone - 以编程方式将 UIBarButtonItem 添加到 UIToolbar?

标签 iphone xcode uibarbuttonitem uitoolbar

这就是我需要的。

我的应用程序有免费版本和付费版本。当付费版本加载时,我的 UIToolBar 上需要 3 个 UIBarButton。当免费版本加载时,我需要 4 个 UIBarButtons。在最右侧的栏按钮上,我需要蓝色,而其余的则默认为黑色。我假设它们之间有灵活的空间以使它们平衡。我已经尝试通过 IB 执行此操作,但我似乎无法使其在正确的间距下工作。正如您所看到的,带有 3 个按钮的底部工具栏与工具栏的间距不均匀。 (我想以编程方式执行此操作)

enter image description here

enter image description here

#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/

相关文章:

iphone - 如何从pdf文件中的FileAttachment Annotation获取图像

ios - 切换方向时 UIBarButtonItem 重置

ios - 如何以编程方式获取 UIBarButtonItem 的状态?

iphone - 云制作的两点之间的距离

iphone - 滑动删除不起作用

php - iPhone OS 图像/照片大小调整工具可更改 EXIF 方向数据

xcode - 从 xcode 登录 azure devops 帐户

objective-c - 将 Objective-C 枚举常量转换为字符串名称

ios - 配置文件中 plist 周围的二进制数据是什么?

ios - UIBarButtonItem 背景图片和设置 Action