UIToolBar背景透明

时间:2012-02-22 05:28:14

标签: ios uinavigationcontroller uitoolbar

我正在尝试将UIToolBar放入UINavigationBar

   UIToolbar* tempFontSizeToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(kPaginationToolBarOriginX,kPaginationToolBarOriginY,kPaginationToolBarWidth,kPaginationToolBarHeight)];

    tempFontSizeToolBar.backgroundColor = [UIColor clearColor];

  // create the array to hold the buttons, which then gets added to the toolbar

   NSMutableArray* buttons = [[NSMutableArray alloc] init];
  [tempFontSizeToolBar setTranslucent:YES];
  UIBarButtonItem *fontSizeBarButtonItem;

  fontSizeBarButtonItem = [[UIBarButtonItem alloc]
                         initWithImage:[UIImage imageNamed:KpreviousPageIcon] style:UIBarButtonItemStylePlain target:self action:@selector(movePreviousPage:)];

  [buttons addObject:fontSizeBarButtonItem];

  [fontSizeBarButtonItem release];fontSizeBarButtonItem = nil;

  fontSizeBarButtonItem = [[UIBarButtonItem alloc]
                         initWithImage:[UIImage imageNamed:KnextpageIcon] style:UIBarButtonItemStylePlain  target:self action:@selector(moveNextPage:)];

  [buttons addObject:fontSizeBarButtonItem];

  [fontSizeBarButtonItem release];fontSizeBarButtonItem = nil;

// stick the buttons in the toolbar
  [tempFontSizeToolBar setItems:buttons animated:NO];

  [buttons release];buttons = nil;

  UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:tempFontSizeToolBar];

  self.navigationItem.rightBarButtonItem = rightBarItem;

UIToolBar的背景颜色是默认的蓝色。 但是我需要工具栏应该是清晰的颜色,以便NavigationBar的背景图像也应该出现在该工具栏中。

请建议我。

3 个答案:

答案 0 :(得分:5)

要使工具栏透明,请使用以下命令:

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

答案 1 :(得分:3)

不确定你在这之后是什么,但是你的代码很乱,我认为你可以毫不费力地得到你想要的东西。无论如何,按钮项不应该有工具栏作为其自定义视图。

如果你的目标是在左边有一个'prev'按钮,在UINavigationBar右边有一个'next'按钮,那么你可以将它们设置为UINavigationItem的leftBarButtonItemrightBarButtonItem 。没有必要的数组。

如果你的目标是让'prev'和'next'彼此相邻并且在UINavigationBar的右侧,那么将它们('next'优先)放在一个数组中,然后使用UINavigationItem的setRightBarButtonItems:animated:

在任何情况下都不需要UIToolbar。您可以根据Apple的文档here将UIToolbar与UINavigationController耦合。它弹出屏幕的底部,可能不是你想要的,但你可以设置它的色调或背景图像。如果您必须将工具栏放在顶部,您可以创建一个并将其手动放置在那里,而不是太难。

祝你好运!

答案 2 :(得分:2)

像这样设置toolbarStyle -1

 tools.barStyle = -1; // clear background