在导航栏的titleView上自定义searchBar

2017 年 12 月 30 日 0 条评论 3.95k 次阅读 0 人点赞

1、初始化一个searchBar,设置好想要的宽、高。

_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,ScreenWidth-40,ScreenHeight-30)];
_searchBar.delegate = self;
_searchBar.placeholder = @"搜索你要的商品";
_searchBar.barTintColor = [UIColor whiteColor];
_searchBar.tintColor = [UIColor blackColor];
_searchBar.layer.borderWidth = 1.0f;
_searchBar.layer.cornerRadius = 5.0f;
_searchBar.layer.borderColor = BMCOLOR(3,107,152).CGColor;

2、新建一个searchBar 的background View,并且添加子view :_searchBar

UIView *searchBarBgView = [[UIView alloc]init];
searchBarBgView.backgroundColor = [UIColor clearColor];
_searchBar.clipsToBounds = YES;
[searchBarBgView addSubview:_searchBar];

3、把searchBarBgView 赋值给titleView

self.navigationItem.titleView = searchBarBgView;
self.navigationItem.titleView.frame = CGRectMake(0,0,ScreenWidth-40,ScreenHeight-30);

技巧在于_searchBar 作为 searchBarBgView的子View,可以调整自己的大小。

雷雷

这个人太懒什么东西都没留下

文章评论(0)

(Spamcheck Enabled)