ios - UIViewController 内的 UIsearchController 使用自动布局

标签 ios objective-c uitableview uisearchbar uisearchcontroller

有没有人成功实现 UIViewController包含一个 UISearchController searchBar和一个 UItableView在使用自动布局布置所有内容时?

我正在尝试实现类似于 1Password 的东西在 iPhone 上做:一个固定的 searchBartableView 之上(不是其 tableHeaderView 的一部分)。当 UISearchController拥有 searchBar被激活,它的 searchBar动画显示范围按钮,从而显示 tableView向下移动一点。

我已经掌握了这个布局的基础知识,可以在这个类中正常工作:

//
//  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 中实例并最初按预期运行,如下面的屏幕截图所示:

Initial Layout Fixed SearchBar

searchBar 出现问题时被激活。它似乎从屏幕上消失了,但在仔细检查 View 后,我们确定它确实在屏幕上,但带有 width。为零。这是一张显示此时呈现的内容的图片:

UISearchBar Gone

我对自动布局没有那么丰富的经验,所以我认为我的约束一定有问题,尽管我在激活 UISearchController 时没有弄乱它们.

有什么方法可以实现吗?

最佳答案

UISearchController 在点击时移动搜索栏,因此它并不总是能很好地满足您的约束条件。

不是直接在搜索栏上设置约束,而是添加一个空的占位符 View 来保存您的搜索栏,然后在 viewDidLoad() 中按程序将搜索栏放入其中。改为在此占位符上设置约束。只需将搜索栏的框架与占位符的边界匹配,并将 translatesAutoresizingMaskIntoConstraints 设置为 true。

抱歉,我不确定这个占位符 View 将如何处理范围按钮的大小变化。希望您能弄清楚如何在更改后让该部分使用自动布局。

关于ios - UIViewController 内的 UIsearchController 使用自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35230208/

相关文章:

ios - 关闭所有当前呈现的 UIAlertControllers

iphone - 将设备更改为ipad retina时如何解决Exe Bad access

iphone - ASIHTTP请求重定向

ios - 代码 : Getting rid of black bar when hiding navigation bar

ios - 如何将 subview 添加到 Storyboard中的自定义 UITableViewCell

ios - map View : didSelectAnnotationView: not functioning properly.

ios - 删除 UITableViewCell 时更新 firebase 中的数据

ios - WKInterfacelabel 检查是否被截断

iPhone iOS 保存从 UIImageJPEGRepresentation() 获取的数据第二次失败 : ImageIO: CGImageRead_mapData 'open' failed

iOS UITableView 在模式关闭后不再调用委托(delegate)