2012-02-22 251 views
1

我想将UIToolBar放入UINavigationBarUIToolBar背景透明

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

不确定你在这里之后,但你的代码是混乱的,我认为你可以得到你想要的东西没有很多努力。无论如何,一个按钮项目不应该有一个工具栏作为其自定义视图。

如果你的目标是在UINavigationBar左边有一个'prev'按钮和一个'next'按钮,那么你可以将它们设置为UINavigationItem的leftBarButtonItemrightBarButtonItem。没有数组必要。如果你的目标是让'prev'和'next'彼此相邻并且在UINavigationBar的右边,然后把它们('next'first)放在一个数组中,然后使用UINavigationItem的setRightBarButtonItems:animated:

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

祝你好运!

+0

我把工具栏上只有导航栏的顶部。我想要的是工具栏应该是透明的,这样工具栏中的按钮就像它直接放置在导航栏中一样。 – Bharathi 2012-02-22 07:48:38

+0

我已经尝试过这些方法。 setRightBarButtonItems:animated:is only only ios 5 .its crashing when running it on version 4. – Bharathi 2012-02-22 07:55:18

+0

那么你要做的是模仿'setRightBarButtonItems:animated:'在iOS 5中? – QED 2012-02-22 16:08:10

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

这模拟了我在iOS 6中遇到的不同问题的正确行为。[我在这个主题上开始的线程在这里。](http://stackoverflow.com/questions/16618410/uitoolbar-buttons-with-uiimage-不完全透明) – 2013-05-17 22:35:30

2

集toolbarStyle -1这样

tools.barStyle = -1; // clear background 
+0

这几乎适用于作为UIViewController根视图的MKMapView之上的UIToolbar。剩下的问题是工具栏的细线仍然被绘制。这是修复:http://stackoverflow.com/questions/19110883/remove-uitoolbar-hairline-in-ios-7 – Barry 2015-07-28 23:38:32