uisearchbar透明背景视图

在UISearchBar默认情况下应该有一个背景视图。 如何隐藏。 我只需要在uisearchbar中searchtextview部分

[[searchBar.subviews objectAtIndex:0] removeFromSuperview]; 

这里有一个适用于iOS 5iOS 6的解决scheme。 我已经检查过,没有必要更改任何其他属性(如将backgroundColor设置为[UIColor clearColor] )以使其工作。

 for (UIView * v in searchBar.subviews) { if ([v isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) { v.superview.alpha = 0; UIView *containerView = [[UIView alloc] initWithFrame:searchBar.frame]; [containerView addSubview:v]; [self.view addSubview:containerView]; } } 

从iOS 5开始,我会推荐以下的方法来避免无用的子视图。 用self.searchBarreplaceself ,如果你是UISearchBarController而不是UISearchBar本身的子类。

 // transparency for UISearchBar self.translucent = YES; self.backgroundImage = [UIImage new]; self.scopeBarBackgroundImage = [UIImage new]; 

作为奖励:

 // transparency for UIToolbar self.translucent = YES; [self setBackgroundImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; [self setShadowImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny]; // transparency for UINavigationBar self.translucent = YES; [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; self.shadowImage = [UIImage new]; // transparency for UITabBar self.backgroundImage = [UIImage new]; self.shadowImage = [UIImage new]; 

更新:从iOS 5.0开始,如果你有一个可用的背景图片,你现在可以做

 searchBar.backgroundImage = someUIImage; 

(只在iOS 6上testing过,所以可能会在5上被窃听!会检查一下。)

布兰登的回答不再起作用(或者对我来说,不pipe怎样),所以我环顾了一下,注意到:

 (gdb) po [searchBar subviews] <__NSArrayM 0x665ab80>( <UISegmentedControl: 0x63aad80; frame = (0 0; 225 44); opaque = NO; layer = <CALayer: 0x6368420>>, <UISearchBarBackground: 0x6347280; frame = (0 0; 225 44); layer = <CALayer: 0x638a230>>, <UISearchBarTextField: 0x6331110; frame = (0 0; 0 0); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x63a20f0>> ) 

第三项是一个自定义的文本字段,据推测这是我想要的,我想,但从超级视图中删除子视图0和1没有修复它奇怪的足够。 我结束了以下工作

 UITextField *sbTextField = (UITextField *)[searchBar.subviews lastObject]; [sbTextField removeFromSuperview]; [self.view addSubview:sbTextField]; sbTextField.frame = searchBar.frame; [searchBar removeFromSuperview]; 

为了使这一点更健壮,最好检查一下这个类是否是文本字段的一个子类,但除非中断,否则我只是简单的把它留给它。

编辑:切换objectAtIndex:2lastObject作为tipycalFlowbuild议。

为未来的访客寻找答案。 我发现以下在iOS 5+中是有效的,因为它不涉及到UISearchBar本身的布局。

 UISearchBar * search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; [search setBackgroundColor:[UIColor clearColor]]; [search setPlaceholder:@"Search"]; for (UIView * v in [search subviews]) { if (![v isKindOfClass:[UITextField class]]) { v.alpha = 0; } } [header addSubview:search]; [search release]; 

UISearchBar背景不能被公共方法隐藏(指定透明颜色,因为tintColor不会帮助)。

如果要精确控制文本字段的样式,则应该使用UITextField。

为了使search栏中的实际文本字段的所有内容都透明,我尝试使用遮罩层(从而显示下面的视图中的内容)。 它的工作原理,但我很紧张,使用它在一个航运应用程序,因为你必须匹配你的面具的形状完全符合UISearchBar文本字段的形状。 如果Apple根本改变文本字段的形状,那么你的面具将不适合,你会看到你不想要的search栏渐变的一部分和/或你不会看到文本字段的部分你确实想要。 这就是说,我是这么做的:

 //first make sure you include core animation so that the compiler will know about your view's layer #import <QuartzCore/QuartzCore.h> //now make a mask. this is basically just a solid colored shape. When you apply the mask, anywhere where the color is solid will become transparent in your view. i used the excellent Opacity (http://likethought.com/opacity/) to generate this code, but you can do it any way you'd like @interface SearchMaskLayer : CALayer { } @end @implementation SearchMaskLayer - (void)drawInContext:(CGContextRef)context { CGRect imageBounds = CGRectMake(0, 0, 310, 34); CGRect bounds = imageBounds; CGFloat alignStroke; CGFloat resolution; CGMutablePathRef path; CGPoint point; CGPoint controlPoint1; CGPoint controlPoint2; UIColor *color; resolution = 0.5 * (bounds.size.width / imageBounds.size.width + bounds.size.height / imageBounds.size.height); CGContextSaveGState(context); CGContextTranslateCTM(context, bounds.origin.x, bounds.origin.y); CGContextScaleCTM(context, (bounds.size.width / imageBounds.size.width), (bounds.size.height / imageBounds.size.height)); // Layer 1 alignStroke = 0.0; path = CGPathCreateMutable(); point = CGPointMake(295.0, 32.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; CGPathMoveToPoint(path, NULL, point.x, point.y); point = CGPointMake(310.0, 17.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; controlPoint1 = CGPointMake(303.229, 32.0); controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; controlPoint2 = CGPointMake(310.0, 25.229); controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); point = CGPointMake(310.0, 17.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; CGPathAddLineToPoint(path, NULL, point.x, point.y); point = CGPointMake(295.0, 2.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; controlPoint1 = CGPointMake(310.0, 8.771); controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; controlPoint2 = CGPointMake(303.229, 2.0); controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); point = CGPointMake(15.0, 2.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; CGPathAddLineToPoint(path, NULL, point.x, point.y); point = CGPointMake(0.0, 17.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; controlPoint1 = CGPointMake(6.771, 2.0); controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; controlPoint2 = CGPointMake(0.0, 8.771); controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); point = CGPointMake(0.0, 17.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; CGPathAddLineToPoint(path, NULL, point.x, point.y); point = CGPointMake(15.0, 32.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; controlPoint1 = CGPointMake(0.0, 25.229); controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; controlPoint2 = CGPointMake(6.771, 32.0); controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); point = CGPointMake(295.0, 32.0); point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; CGPathAddLineToPoint(path, NULL, point.x, point.y); CGPathCloseSubpath(path); color = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]; [color setFill]; CGContextAddPath(context, path); CGContextFillPath(context); CGPathRelease(path); } 

稍后在你的一个方法中,应用掩码

 - (void)viewDidLoad { [super viewDidLoad]; SearchMaskLayer *maskLayer = [[SearchMaskLayer alloc] init]; [maskLayer setFrame:CGRectMake(0, 0, 310, 34)]; [maskLayer setPosition:CGPointMake(162,21)]; [maskLayer setNeedsDisplay]; [self.searchBar.layer setNeedsDisplay]; [self.searchBar.layer setMask:maskLayer]; [maskLayer release]; } 

为了看看它的样子,我把图层的位置搞乱了,但是你可以计算出这些大小和位置来适应你的需求。

这是我的博客文章关于这个问题。

我跟着类似的方法去了Weaverfish。 不过,我将自己设置的子视图alpha属性封装在一个类中。

您可以如下所示inheritanceUISearchBar

在UITransparentBackgroundSearchBar.h中:

 #import <UIKit/UIKit.h> @interface UITransparentBackgroundSearchBar : UISearchBar @end 

并在UITransparentBackgroundSearchBar.m:

 #import "UITransparentBackgroundSearchBar.h" @implementation UITransparentBackgroundSearchBar -(void)didAddSubview:(UIView *)subview { if (![subview isKindOfClass:[UITextField class]]) { subview.alpha = 0; } } @end 

在故事板中,将search栏类设置为UITransparentBackgroundSearchBar和voilà。

现在,我是iOS开发新手,所以我不能说这是防弹的。 它似乎工作,它避免了代码重复,如果我需要在另一个视图相同的透明背景。

除了卡列之外,没有任何答案适用于我。 但略有改进。

 UITextField *sbTextField = (UITextField *)[self.mSearchBar.subviews lastObject]; [sbTextField removeFromSuperview]; [self.view addSubview:sbTextField]; CGRect sbFrame = self.mSearchBar.frame; // Set the default height of a textfield sbFrame.size.height = 31; /* 8 is the top padding for textfield inside searchbar * You may need to add a variable to 8 according to your requirement. */ sbFrame.origin.y = 8+ self.mSearchBar.superview.frame.origin.y; sbTextField.frame = sbFrame; [sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin]; //to support different orientations. [self.mSearchBar removeFromSuperview]; 

+1给Kalle。 (竖起大拇指)

通过为backgroundImage属性设置1px * 1px的透明图像很容易。 在ios 5,6上testing

 searchBar.backgroundImage = [UIImage imageNamed:@"transparentImage.png"]; 

布兰登的答案仍然有效,但与蒂莫西·格罗特提到的条件。 这就像删除两个较低的子视图一样简单,并确保风格设置为半透明的黑色(我在IB做过,但我假设在代码中工作)。

出于好奇,为什么你们认为这不会被批准? 该解决scheme只使用公共API。

没有黑客在这里:

 if (([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] == NSOrderedAscending)) { UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0); UIImage *blank = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); searchBar.backgroundImage = blank; searchBar.backgroundColor = [UIColor clearColor]; } else { searchBar.barTintColor = [UIColor clearColor]; }