【问题标题】:UIToolBar background transparentUIToolBar 背景透明
【发布时间】:2012-02-22 05:28:13
【问题描述】:

我正在尝试将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 的背景颜色是默认的蓝色。 但是我需要工具栏应该是清晰的颜色,以便导航栏的背景图像也应该出现在该工具栏中。

请给我建议。

【问题讨论】:

    标签: ios uinavigationcontroller uitoolbar


    【解决方案1】:

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

    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];
    

    【讨论】:

    【解决方案2】:

    不知道你在这里追求什么,但你的代码很乱,我认为你可以不费吹灰之力就得到你想要的。无论如何,一个按钮项不应该有一个工具栏作为它的自定义视图。

    如果您的目标是在 UINavigationBar 的左侧有一个“上一个”按钮,在右侧有一个“下一个”按钮,那么您可以将它们设置为 UINavigationItem 的leftBarButtonItemrightBarButtonItem。不需要数组。

    如果您的目标是让 'prev' 和 'next' 彼此相邻并位于 UINavigationBar 的右侧,则将它们('next' first)放入一个数组中,然后使用 UINavigationItem 的setRightBarButtonItems:animated:

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

    祝你好运!

    【讨论】:

    • 我只将工具栏放在导航栏的顶部。我想要的是工具栏应该是透明的,以便工具栏中的按钮看起来就像它直接放在导航栏中一样。
    • 我已经尝试过这些方法。 setRightBarButtonItems:animated: 仅支持 ios 5 。它在版本 4 上运行时崩溃。
    • 那么您要做的是在 iOS setRightBarButtonItems:animated:?
    【解决方案3】:

    像这样设置toolbarStyle -1

     tools.barStyle = -1; // clear background
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 2021-09-20
    • 2019-02-07
    • 2020-10-15
    • 2017-06-22
    • 2010-10-20
    • 2011-02-01
    • 2013-12-10
    相关资源
    最近更新 更多