UIViewController 内的 UIsearchController 使用自动布局
UIsearchController inside UIViewController using Auto Layout
有没有人在使用自动布局布置所有内容时成功实施了包含 UISearchController
searchBar
和 UItableView
的 UIViewController
?
我正在尝试实现类似于 1Password 在 iPhone 上所做的事情:在 tableView
之上固定 searchBar
(不是它的一部分tableHeaderView
)。当拥有 searchBar
的 UISearchController
被激活时,它的 searchBar
会动画显示范围按钮,因此 tableView
会向下移动一点。
我已经掌握了此布局的基础知识,可以正常使用 class:
//
// ViewController.m
// UISearchControllerIssues
//
// Created by Aloha Silver on 05/02/16.
// Copyright © 2016 ABC. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UISearchResultsUpdating, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupTableView];
[self setupSearchInterface];
[self setupConstraints];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.searchController.searchBar sizeToFit];
}
- (void)setupTableView {
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.tableView];
}
- (void)setupSearchInterface {
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = @[@"One", @"Two"];
self.searchController.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
self.searchController.searchResultsUpdater = self;
[self.view addSubview:self.searchController.searchBar];
}
- (void)setupConstraints {
NSDictionary *layoutViews = @{@"searchBar": self.searchController.searchBar, @"tableView": self.tableView, @"topLayoutGuide": self.topLayoutGuide};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|" options:0 metrics:nil views:layoutViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:layoutViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[searchBar(44)][tableView]|" options:0 metrics:nil views:layoutViews]];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSLog(@"Update should happen here");
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"Cell number %ld", (long)indexPath.row];
return cell;
}
@end
它嵌入在一个 UINavigationController
实例中,最初按预期运行,如下面的屏幕截图所示:
searchBar
激活时出现问题。它似乎从屏幕上消失了,但在仔细检查视图后,我们确定它确实在屏幕上,但 width
为零。这是一张显示此时呈现的内容的图片:
我对自动布局没有那么多经验,所以我认为我的约束一定有问题,尽管我在激活 UISearchController
.[=29 时并没有弄乱它们=]
有什么方法可以实现吗?
UISearchController 会在点击时四处移动搜索栏,因此它并不总是能很好地满足您的约束条件。
不要直接在搜索栏上设置约束,而是添加一个空的占位符视图来容纳您的搜索栏,然后在 viewDidLoad()
中按程序将搜索栏放入其中。改为在此占位符上设置约束。只需将搜索栏的框架与占位符的边界相匹配,并将 translatesAutoresizingMaskIntoConstraints
设置为 true。
抱歉,我不确定此占位符视图将如何处理范围按钮的大小更改。希望您能弄清楚如何在更改后让该部分使用自动布局。
有没有人在使用自动布局布置所有内容时成功实施了包含 UISearchController
searchBar
和 UItableView
的 UIViewController
?
我正在尝试实现类似于 1Password 在 iPhone 上所做的事情:在 tableView
之上固定 searchBar
(不是它的一部分tableHeaderView
)。当拥有 searchBar
的 UISearchController
被激活时,它的 searchBar
会动画显示范围按钮,因此 tableView
会向下移动一点。
我已经掌握了此布局的基础知识,可以正常使用 class:
//
// ViewController.m
// UISearchControllerIssues
//
// Created by Aloha Silver on 05/02/16.
// Copyright © 2016 ABC. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UISearchResultsUpdating, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupTableView];
[self setupSearchInterface];
[self setupConstraints];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.searchController.searchBar sizeToFit];
}
- (void)setupTableView {
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.tableView];
}
- (void)setupSearchInterface {
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = @[@"One", @"Two"];
self.searchController.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
self.searchController.searchResultsUpdater = self;
[self.view addSubview:self.searchController.searchBar];
}
- (void)setupConstraints {
NSDictionary *layoutViews = @{@"searchBar": self.searchController.searchBar, @"tableView": self.tableView, @"topLayoutGuide": self.topLayoutGuide};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|" options:0 metrics:nil views:layoutViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:layoutViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[searchBar(44)][tableView]|" options:0 metrics:nil views:layoutViews]];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSLog(@"Update should happen here");
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"Cell number %ld", (long)indexPath.row];
return cell;
}
@end
它嵌入在一个 UINavigationController
实例中,最初按预期运行,如下面的屏幕截图所示:
searchBar
激活时出现问题。它似乎从屏幕上消失了,但在仔细检查视图后,我们确定它确实在屏幕上,但 width
为零。这是一张显示此时呈现的内容的图片:
我对自动布局没有那么多经验,所以我认为我的约束一定有问题,尽管我在激活 UISearchController
.[=29 时并没有弄乱它们=]
有什么方法可以实现吗?
UISearchController 会在点击时四处移动搜索栏,因此它并不总是能很好地满足您的约束条件。
不要直接在搜索栏上设置约束,而是添加一个空的占位符视图来容纳您的搜索栏,然后在 viewDidLoad()
中按程序将搜索栏放入其中。改为在此占位符上设置约束。只需将搜索栏的框架与占位符的边界相匹配,并将 translatesAutoresizingMaskIntoConstraints
设置为 true。
抱歉,我不确定此占位符视图将如何处理范围按钮的大小更改。希望您能弄清楚如何在更改后让该部分使用自动布局。