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)