2011-06-06 93 views
9

我写了下面的代码,使我的工具栏透明。透明UIToolbar

[mtoolbar setBackgroundColor:[UIColor clearColor]]; 

如何使UIToolbar透明?

+0

你想一个真正透明的工具栏,或者只是一个半透明的一个? – 2011-06-06 13:05:50

+0

[如何在iOS7中绘制透明的UIToolbar或UINavigationBar](http:// stackoverflow。com/questions/18969248/how-to-draw-a-transparent-uitoolbar-or-uinavigationbar-in-ios7) – 2014-06-27 13:28:46

回答

11

您可以将属性translucent设置为YES并查看是否有帮助。

+0

我想我们需要设置属性yes – 2011-06-06 12:15:13

+1

编辑:)只是一个快速猜测,我还没有之前直接使用工具栏。 – Luke 2011-06-06 13:03:11

+0

它不适合我。 UIToolbar的背景颜色现在是黑色透明的。但我需要它清晰的颜色。 – Bharathi 2012-02-22 05:54:44

4

检查下面的代码

[myToolbar setBarStyle:UIBarStyleBlack]; 
[myToolbar setTranslucent:YES]; 

@Brandon博德纳尔两者已经回答了在下方,以便张贴。

Couldn't UIToolBar be transparent?

,你也可以使用不同的方法

Transparent UIToolBar

+0

我设置了alpha 0.7 – 2011-06-06 13:22:44

1

iOS 5中的以下作品(和iOS 6 beta 4版本后,虽略有顶部阴影仍可见有)。

请注意: 制作UIToolbar或UINavigationBar的透明很少是一个好主意,并修改以这样的方式苹果的UIKit元素势必打破迟早的事。

TransparentToolbar.h

#import <UIKit/UIKit.h> 

@interface TransparentToolbar : UIToolbar 

@end 

TransparentToolbar.m

#import "TransparentToolbar.h" 

@implementation TransparentToolbar 

-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index 
{ 
    // This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view. 

    if (index != 0) 
    { 
     [super insertSubview:view atIndex:index]; 
    } 
    else 
    { 
     // insert your custom background view, if you want to 
    } 
} 


@end 

编辑:在iOS中5+,它也可以简单地设置和backgroundImage(其可以是透明的)。这当然是“更清洁”的解决方案,但比自定义UIView更不灵活。

[someToolbar setBackgroundImage:[UIImage imageNamed:@"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 
2
for (UIView * sv in [toolBar subviews]) 
{ 
    [sv removeFromSuperview]; 
} 

;)在任何iOS

5

设置属性translucentYES不会在iOS 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]; 
10
[self.toolbar setBackgroundImage:[UIImage new] 
       forToolbarPosition:UIToolbarPositionAny 
         barMetrics:UIBarMetricsDefault]; 

[self.toolbar setBackgroundColor:[UIColor clearColor]]; 
1

这个工作对我来说的iOS 6和7:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0); 
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 
+0

仍然显示边框。有没有修复它? – Yevgeni 2014-04-28 11:36:56