在navigationBar上添加searchBar 并设置字体

来源:互联网 发布:网络三国游戏 编辑:程序博客网 时间:2024/05/22 15:30
//添加搜索框- (void)addSearchBar{    CGRect mainViewBounds = self.navigationController.view.bounds;    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(CGRectGetWidth(mainViewBounds)/2-((CGRectGetWidth(mainViewBounds)-120)/2), CGRectGetMinY(mainViewBounds)+27, CGRectGetWidth(mainViewBounds)-100, 30)];    _searchBar.delegate = self;    _searchBar.showsCancelButton = NO;    _searchBar.searchBarStyle = UISearchBarStyleMinimal;    _searchBar.returnKeyType=UIReturnKeySearch;    _searchBar.placeholder = @"编号/申请人/所在部门";    //修改placeholder 字体    UITextField * searchField = [_searchBar valueForKey:@"_searchField"];    [searchField setValue:kSubTextColor forKeyPath:@"_placeholderLabel.textColor"];    [searchField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];}

注意:在跳转页面的时候必须移除掉,否则,会在下个页面继续显示,在页面出现时再添加上

- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    //添加搜索框    [self.navigationController.view addSubview: _searchBar];}- (void)viewWillDisappear:(BOOL)animated{    [_searchBar resignFirstResponder];    [_searchBar removeFromSuperview];}

从页面显示上来看,searchBar添加在了searchBar 上,实际上添加在了当前页的View上。

原创粉丝点击