修改UISearchBar背景

本文介绍如何通过多种方式修改UISearchBar的外观,包括隐藏背景、仅显示UITextField等,并提供了详细的代码示例。

UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性,在此我总结了几个方法去假改它。

1. 只显示UITextField.采用了layer mask.代码如下:

//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); } 然后在你的controller中应用这个mask layer到你的UISearchBar - (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]; }

2.隐藏背景。非官方的方法。

for (UIView *subview in searchBar.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { [subview removeFromSuperview]; break; } }

3. 改变UISearchBar外观。 你可以子类化或category UISearchBar,然后实现两个方法,见代码。

- (void)drawRect:(CGRect)rect { // UIImage *image = [UIImage imageNamed: @"background.png"]; // [image drawInRect:rect]; } - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { UIImage *img = [UIImage imageNamed: @"background.png"]; UIImageView *v = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease]; [v setImage:img]; v.bounds = CGRectMake(0, 0, img.size.width, img.size.height); NSLog([NSString stringWithFormat:@"%f:%f",img.size.width, img.size.height]); NSArray *subs = self.subviews; for (int i = 0; i < [subs count]; i++) { id subv = [self.subviews objectAtIndex:i]; if ([subv isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { CGRect viewRect = [subv frame]; [v setFrame:viewRect]; [self insertSubview:v atIndex:i]; } } [v setNeedsDisplay]; [v setNeedsLayout]; }

UISearchBar也是一个UIView,所以你可以像对待UIView一样对待它。

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值