您的位置:首页 > 移动开发 > IOS开发

自定义导航searchBar

2016-03-10 10:02 441 查看
源码如下:

- (void)configureSearchBar
{
UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth-60*2, 30)];
searchView.clipsToBounds = YES;
searchView.layer.cornerRadius = 6;
searchView.layer.borderWidth = 1;
searchView.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    searchView.backgroundColor = color;

_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth-60*2, 30)];
_searchBar.placeholder = @"请输入城市名";
//iOS7去掉底色
[[[[_searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview ];
[_searchBar setBackgroundColor:[UIColor clearColor]];

_searchBar.delegate = self;
//    [_searchBar becomeFirstResponder];

[searchView addSubview:_searchBar];

[self.navigationItem.titleView sizeToFit];

self.navigationItem.titleView = searchView;
}


实际运行效果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息