自定义UISearchBar的左边放大镜

UISearchBar是在iOS开发中经常用到的一个组件,那么我们如何来实现一个类似AMAP中的SearchBar呢?

我们可以首先来观察一下,这个searchBar和默认的有什么区别?

默认的SearchBar,如下图:

首先有起点和终点这样的文字提示,默认的是一个搜索图标。

其次是文本框的边框是圆角矩形,默认的是圆角为半圆型。

那么我们实现自定义的UISearchBar就需要修改这两项。

实现的方式就是重新实现UISearchBar的layoutSubviews方法

- (void)layoutSubviews {
	UITextField *searchField;
	NSUInteger numViews = [self.subviews count];
	for(int i = 0; i < numViews; i++) {
		if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
			searchField = [self.subviews objectAtIndex:i];
		}
	}
	if(!(searchField == nil)) {
		searchField.textColor = [UIColor redColor];
	//	[searchField setBackground: [UIImage imageNamed:@"esri.png"] ];
		[searchField setBorderStyle:UITextBorderStyleRoundedRect];
		UIImage *image = [UIImage imageNamed: @"esri.png"];
		UIImageView *iView = [[UIImageView alloc] initWithImage:image];
		searchField.leftView = iView;
		[iView release];

	}

	[super layoutSubviews];
}

最终效果如下:

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值