UIViewController中的UIsearchController使用自动布局

Alo*_*ver 4 objective-c uitableview uisearchbar ios uisearchcontroller

任何人都已经成功实施UIViewController该contais既有UISearchController searchBarUItableView在使用自动布局铺设东西展现出来?

我正在努力实现类似于1Password在iPhone上所做的事情:固定searchBar在一个tableView(不是它的一部分)之上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
Run Code Online (Sandbox Code Playgroud)

它嵌入在一个UINavigationController实例中,最初按预期运行,如下面的屏幕截图所示:

初始布局 修复了SearchBar

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

UISearchBar已经过去了

我不熟悉Auto Layout,所以我一直认为我的约束肯定有问题,虽然我在激活时不会弄乱它们UISearchController.

有没有办法使这项工作?

Dav*_*ton 10

UISearchController在点击时会移动搜索栏,因此它并不总能很好地适应您的约束.

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

抱歉,我不确定此占位符视图如何使用范围按钮处理大小更改.希望您可以弄清楚如何在更改后使用自动布局使该部件工作.