2

这就是我需要的。

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

在此处输入图像描述

在此处输入图像描述

#ifdef LITE_VERSION


#else

[buyButton removeFromSuperview];

#endif
4

4 回答 4

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

尝试使用它可能会对您有所帮助

于 2012-06-15T07:09:09.137 回答
0

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

if(AppPaid) { 
    NSMutableArray *items = [YourToolBar.items mutableCopy];
    [items removeObject:yourBuyButton];
    YourToolBar.items = items;
    [items release];
  }
于 2012-06-15T05:09:05.003 回答
0
IBOutlet UITabBarItem * item1;

像这样创建 4 个项目...提供指向您的 xib 的链接,以便我们可以tabbarItem在您需要时隐藏所需...我希望这会有所帮助

于 2012-06-15T05:54:52.800 回答
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

这是我在我的一个应用程序中使用的一些代码,用于-1在运行时用特定按钮替换在 IB 中标记的项目,具体取决于显示的视图

于 2012-06-27T13:34:36.047 回答