在UISearchBar默认情况下,应该出现背景视图。如何隐藏。我只需要搜索textview部分,在uisearchbaruisearchbar透明背景视图
回答
UISearchBar背景不能被公共方法隐藏(指定一个透明的颜色作为tintColor
不会帮助)。
,如果你想在文本字段的风格精确的控制你应该使用的UITextField。
我尝试,以便利用掩模层,使在搜索栏透明的(从而示出了从下方视图内容)周围的实际文本字段的一切。它的工作原理,但我很紧张,因为你必须将你的面具的形状与UISearchBar中的文本字段的形状完全匹配,因此在运输应用中使用它。如果苹果根本改变了文本字段的形状,那么你的蒙版将不适合,你会看到你不想要的搜索栏渐变的一部分和/或你不会看到部分文本字段你确实需要。这就是说,这里是我是如何做到的:
//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];
}
我捏造层的定位只是为了看看它会是什么样的面具,但你可以计算出这些尺寸和位置以满足你的需求。
如果遇到编译错误,请确保包含quartzcore框架:http://stackoverflow.com/questions/2968071/cant-compile-code-when-working-with-calayer – Ying 2011-05-18 19:42:30
圣神,这个作品!它的边缘粗糙,所以卡尔佩林,如果你有一个更新的版本,请分享:)在此期间,我会尝试看看我是否可以捅过来使这符合我的需求。 – Ying 2011-05-18 19:46:13
不幸的是,以某种方式附加这个蒙版可以防止uisearchbar在视图大小发生变化时自动调整大小,比如纵向显示。不适合我,但非常非常酷! – Ying 2011-05-18 19:56:22
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
更新:通过iOS 5.0的,如果你有一个可用的背景图片,你可以现在就做
searchBar.backgroundImage = someUIImage;
(仅在iOS 6测试,因此可在5虽然被窃听先查!左右)
布兰登的回答不再有效(或不适合我,反正),所以我四处张望了一下,并指出:
(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>>
)
第3项是一个自定义文本字段,据推测它是我想要的那个,我想,但是从超级视图中删除子视图0和1并不足以解决它。我结束了;下面,确实工作:
UITextField *sbTextField = (UITextField *)[searchBar.subviews lastObject];
[sbTextField removeFromSuperview];
[self.view addSubview:sbTextField];
sbTextField.frame = searchBar.frame;
[searchBar removeFromSuperview];
为了让这个有点更健壮,它可能是一个好主意,检查类文本字段和这样的一个子类,但除非它休息我只是简单的离开。
编辑:切换到objectAtIndex:2
为lastObject
建议tipycalFlow。
+1,很好!这很奇怪,但删除索引为2的子视图不应该一直工作,因为数组大小显然不固定。更好的方法是'UITextField * sbTextField =(UITextField *)[searchBar.subviews lastObject];' – tipycalFlow 2011-11-08 06:20:29
好点。更新! – Kalle 2011-12-01 09:51:57
+1。需要小修改。看到我的答案。 – thesummersign 2012-05-18 05:58:52
布兰登的答案仍然有效,但由蒂莫西·格鲁特提到的条件。这很简单,只需删除两个较低的子视图,并确保该样式设置为半透明黑色(我在IB中完成,但我假设在代码中执行此操作)。
只是出于好奇,为什么你们认为这不会得到批准?该解决方案只使用公共API。
的答案都不为我工作,除了卡勒年代。 但略有改进。
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。(竖起大拇指)
为未来的访客寻找答案。我发现以下内容在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];
为我工作!非常感谢! – acecapades 2012-09-12 08:31:40
这是我的blog post有关这个问题。
我对Weaverfish采取了类似的方法。但是,我封装了将子视图alpha属性设置在自己的类中的过程。
你也可以继承UISearchBar
如下:
:
#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
在你的故事板,搜索栏类设置为被UITransparentBackgroundSearchBar和voilà。
现在,我是iOS开发新手,所以我不能说这是防弹的。它似乎工作,它避免了代码重复,如果我需要在另一个视图中相同的透明背景。
这里有一个解决方案,适用于iOS 5和iOS 6。我已经检查过,没有必要更改任何其他属性(如将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];
}
}
这是必要的疯狂。谢谢! – pir800 2012-10-10 16:23:40
+1尝试像其他5个(设置图像等..)没有工作,但这一个在ios 6马上工作! – fellowworldcitizen 2013-02-05 14:13:34
精彩!!我尝试了很多东西,但这工作:) – Pruthvid 2013-03-04 20:14:16
开始的iOS 5,我建议做以下的方式,从而避免了与无证子视图搞乱。如果您要继承UISearchBarController而不是UISearchBar本身,请将self
替换为self.searchBar
。
// 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];
令人惊叹的答案。非常感谢您更新这篇旧文章。幸运的是,我将链接保存在我的代码中,并因此与iOS7兼容。 – 2013-09-14 03:17:51
在iOS 8中没有快乐:( – 2015-02-19 08:06:39
没有黑客在这里:
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];
}
- 1. iPad/iPhone uiSearchbar透明背景
- 2. 前视图背景透明
- 3. UISearchBar最小风格有透明背景
- 4. iphone sdk透明子视图背景
- 5. ActionSheet不透明的背景视图
- 6. 透明背景的视图presentViewController
- 7. 透明背景透明图像问题
- 8. 透明背景
- 9. 图像背景透明GIMP
- 10. 使图像背景透明
- 11. 透明背景图像
- 12. 透明背景图(matlab)
- 13. CSS透明背景图片
- 14. wxWidgets透明背景图片?
- 15. 透明背景图像
- 16. JavaScript背景透视图库
- 17. html透明背景
- 18. 与透明背景
- 19. Iframe透明背景
- 20. webview透明背景
- 21. NSTableCellView透明背景
- 22. UIToolBar背景透明
- 23. Pygame透明背景
- 24. AS3透明背景
- 25. JOGL透明背景
- 26. PHP:透明背景
- 27. Div背景透明
- 28. 透明JFrame背景
- 29. AChartEngine透明背景
- 30. UWP:MapControl透明背景
这是非常重要的,如果你仍然支持的iOS 4.x版 – slatvick 2012-04-12 16:52:21