搜索栏的右边缘比左边宽,我不知道为什么?这不好酷。请帮帮我。非常感谢你。
这是我的代码:
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *strongSearchDisplayController;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.searchBar.backgroundColor = [UIColor clearColor];
self.searchBar.placeholder = @"搜索";
self.searchBar.delegate = self;
[self.searchBar sizeToFit];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREENWIDTH, SCREENHEIGHT - NAVIGATIONBARHEIGHT - TABBARHEIGHT ) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.tableHeaderView = self.searchBar;
self.strongSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
答案 0 :(得分:0)
我的建议是以这种方式实现搜索栏:
CGRectZero相当于(0,0,0,0)
此外,最好将搜索栏添加到视图中。像这样:
- (void) viewDidLoad:(BOOL)animated {
UIView *searchbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; //This adds a container that will hold the search bar.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
self.searchBar.delegate = self;
[searchbarView addSubview:self.searchBar];
self.tableView.tableHeaderView = searchbarView; //Inserts UIView into the header of self.tableView
}
答案 1 :(得分:0)
您可以进行以下操作:
向表中添加等于searchBar height的顶部偏移:
self.tableView.contentInset = UIEdgeInsetsMake(searchController.searchBar.frame.height, 0, 0, 0);
将searchBar直接添加到tableView,而不是tableHeaderView:
// remove this
self.tableView.tableHeaderView = self.searchBar;
// insert this
[self.tableView addSubview:self.searchBar];
请确保searchBar rect具有正确的原点(0,0)
注意:它会解决您的问题,但searchBar的行为会与原版略有不同:默认情况下会显示搜索栏,搜索不会自动隐藏当它关闭时等吧