使用自动布局的UIViewController内的UIsearchController [英] UIsearchController inside UIViewController using Auto Layout

查看:54
本文介绍了使用自动布局的UIViewController内的UIsearchController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都成功实现了 UIViewController ,同时放置了所有内容的同时包含了 UISearchController searchBar UItableView 使用自动版式?

我正在尝试实现类似于

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

我对Auto Layout的经验不多,所以我离开了,我的约束一定有问题,尽管在激活 UISearchController 时我不会惹恼他们.

有什么办法可以使这项工作成功吗?

解决方案

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

不是直接在搜索栏上设置约束,而是添加一个空的占位符视图以容纳您的搜索栏,然后将其按程序放在 viewDidLoad()中.而是在此占位符上设置约束.只需将搜索栏的框架与占位符的边界匹配,然后将 translatesAutoresizingMaskIntoConstraints 设置为true即可.

抱歉,我不确定此占位符视图将如何使用范围按钮处理大小更改.希望您能弄清楚更改后如何使该零件与自动布局一起使用.

Has anyone been successful implementing a UIViewController that contais both a UISearchController searchBar and a UItableView while laying everything out using Auto Layout?

I'm trying to achieve something similar to what 1Password does on the iPhone: a fixed searchBar on top of a tableView (not part of its tableHeaderView). When the UISearchController that owns the searchBar gets activated, its searchBar animates to show the scope buttons and thus the tableView moves down a bit.

I have got the basics of this layout working correctly with this 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

It is embedded in a UINavigationController instance and initially runs as expect, like the following screenshots show:

Trouble arises when the searchBar gets activated. It seems to disappear from screen, but after carefully inspecting the view, we determine that it is actually onscreen, but with a width of zero. Here's a picture showing what is presented at this time:

I'm not that experienced with Auto Layout, so I'm left thinking there must be something wrong with my constraints, although I don't mess with them when activating the UISearchController.

Is there any way of making this work?

解决方案

The UISearchController moves the search bar around when it's tapped, so it doesn't always play well with your constraints.

Instead of setting your constraints directly on the search bar, add an empty placeholder view that will hold your search bar and then place the search bar in it procedurally in viewDidLoad(). Set your constraints on this placeholder instead. Just match the search bar's frame to the placeholder's bounds and leave translatesAutoresizingMaskIntoConstraints set to true.

Sorry, I'm not sure how this placeholder view will handle the size change with the scope buttons. Hopefully you can figure out how to get that part working with auto layout after the change.

这篇关于使用自动布局的UIViewController内的UIsearchController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆