2012-06-15 55 views
1

我已经做了大量的研究并阅读了很多关于这个主题的文章。我的需求似乎没有任何工作。这是我需要的。以编程方式将UIBarButtonItem添加到UIToolbar?

我有一个免费版本和我的应用程序的付费版本。当付费版本加载时,我需要在我的UIToolBar上使用3个UIBarButton。当免费版本加载时,我需要4个UIBarButtons。在最右边的barButton上,我需要淡蓝色,而其余的是默认黑色。而且我假设他们之间的灵活空间可以将它们排除在外。我试图通过IB来做这件事,但我似乎无法让它与间距正确工作。如您所见,带3个按钮的底部工具栏与工具栏间的距离不均匀。任何帮助将是伟大的! (我想做到这一点programmically)

enter image description here

enter image description here

#ifdef LITE_VERSION 


#else 

[buyButton removeFromSuperview]; 

#endif 
+0

我可以有一些代码更清晰 –

+0

希望可以帮助 – Jason

回答

11
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 

尝试使用这可能会帮助你

+0

好的,我能够让它显示出来,但是这会推动我的视图并将工具栏放在顶部,我需要在视图顶部和底部。 – Jason

+0

好的,我通过更改一些代码来让UIToolBar在底部显示。但是按钮之间应该有一些灵活的空间,所以当我移除按钮时,按钮甚至会出现在工具栏上。 – Jason

+0

灵活的空间不在那里? – Jason

0

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

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

是的,我试过这个代码。我无法得到“YourToolBar”没有错误。我还是这个新手。 – Jason

+0

在这里发表你的代码! – Mani

+0

我已经在上面添加了我的代码,一切都是通过IB来完成的,所以IBOutlets是唯一的其他东西。你可以看到我有多少个按钮。我想如果你能解释我将如何“连接”你的代码中的“YourToolBar”部分,我想这就是我的问题所在。 – Jason

0

这样创造4项...给链接到您的厦门国际银行,这样我们可以隐藏需要tabbarItem当过你想......我希望这将有助于

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的项目,这取决于哪个观点是对显示

相关问题