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

时间:2012-06-15 04:38:56

标签: iphone xcode uibarbuttonitem uitoolbar

这就是我需要的。

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

enter image description here

enter image description here

#ifdef LITE_VERSION


#else

[buyButton removeFromSuperview];

#endif

4 个答案:

答案 0 :(得分:10)

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 :(得分:0)

首先在工具栏上添加“购买”按钮。绑定和合成。

if(AppPaid) { 
    NSMutableArray *items = [YourToolBar.items mutableCopy];
    [items removeObject:yourBuyButton];
    YourToolBar.items = items;
    [items release];
  }

答案 2 :(得分:0)

IBOutlet UITabBarItem * item1;

像这样创建4个项目...给你的xib链接,以便我们可以随时隐藏所需的tabbarItem ...我希望这会有所帮助

答案 3 :(得分:0)

默认情况下将购买按钮添加到工具栏中,并使用一些唯一值(例如-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标记的项目,具体取决于显示的视图