UIToolBar background transparent

2019-04-02 09:48发布

问题:

I am trying to place UIToolBar in 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;

The background color of that UIToolBar is the default Blue. But I need the Toolbar should be in clear color so that the NavigationBar's background image should appear in that Toolbar also.

Pls suggest me.

回答1:

Not sure what you're after here, but your code is messy, and I think you can get what you want without so much effort. Anyway, a button item should not have a toolbar as its custom view.

If your goal is to have a 'prev' button on the left and a 'next' button on the right of a UINavigationBar, then you can just set them as the UINavigationItem's leftBarButtonItem and rightBarButtonItem. No array necessary.

If your goal is to have 'prev' and 'next' adjacent to each other and on the right side of the UINavigationBar, then put them ('next' first) in an array and then use UINavigationItem's setRightBarButtonItems:animated:.

In neither case is a UIToolbar necessary. You can couple a UIToolbar with a UINavigationController as per Apple's docs here. It pops up at the bottom of the screen, perhaps not what you want, but you can set its tint or background image. If you must have the toolbar at top, you can create one and place it there manually, not too hard.

Good luck!



回答2:

To make a toolbar transparent, use the following:

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


回答3:

set toolbarStyle -1 like this

 tools.barStyle = -1; // clear background