也可能是状态栏高度设置不准确,总之这个方案会有点问题。我们换另外一个思路,将导航条设置为titleview,这里需要特别注意,如果直接将CCSearchBar赋值给self.navigationItem.titleView,搜索框的长度会不受控制,需要封装一层,将搜索框添加到一个view中,然后将该view赋值给titleview。
-
(CCSearchBar *)searchBar{
if (!_searchBar) {
_searchBar = [[CCSearchBar alloc]init]; _searchBar.backgroundColor = [UIColor clearColor]; _searchBar.textfiledBgColor = K_CC_GRAY_BG_COLOR; _searchBar.placeholder = K_CC_LOCAL_STR(@"home.search"); _searchBar.placeHolderFontSize = 14; _searchBar.placeHolderTextColor = K_CC_TEXT_GRAY_COLOR; _searchBar.textPosition = SearchBarTextPositionCenter; _searchBar.textColor = K_CC_COLOR_STRING(@"17315C"); _searchBar.frame = CGRectMake(0, 5, K_CC_SCREEN_WIDTH-146, 34); _searchBar.delegate = self;
}
return _searchBar;
}
- (UIView *)toolSearch
{
if (!_toolSearch) {
_toolSearch=[[UIView alloc]initWithFrame:CGRectMake(0, 0, K_CC_SCREEN_WIDTH - 106, 44)];
_toolSearch.backgroundColor = [UIColor clearColor];
[_toolSearch addSubview:self.searchBar];
}
return _toolSearch;
}
self.navigationItem.titleView=self.toolSearch;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.searchBar becomeFirstResponder];
});