Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用自动布局的UIViewController中的UIsearchController_Ios_Objective C_Uitableview_Uisearchbar_Uisearchcontroller - Fatal编程技术网

Ios 使用自动布局的UIViewController中的UIsearchController

Ios 使用自动布局的UIViewController中的UIsearchController,ios,objective-c,uitableview,uisearchbar,uisearchcontroller,Ios,Objective C,Uitableview,Uisearchbar,Uisearchcontroller,是否有人成功地实现了一个UIViewController,它既包含UISearchControllersearchBar又包含UItableView,同时使用自动布局布局所有内容 我正试图实现与iPhone类似的功能:在表格视图(不是其表格头视图的一部分)上安装一个固定的搜索栏。当拥有searchBar的UISearchController被激活时,其searchBar动画显示范围按钮,因此tableView会向下移动一点 我已经掌握了该布局的基本知识,可以在本课程中正确使用: // // V

是否有人成功地实现了一个
UIViewController
,它既包含
UISearchController
searchBar
又包含
UItableView
,同时使用自动布局布局所有内容

我正试图实现与iPhone类似的功能:在
表格视图
(不是其
表格头视图
的一部分)上安装一个固定的
搜索栏。当拥有
searchBar
UISearchController
被激活时,其
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
//
//ViewController.m
//UISearchControllerIssues
//
//由Aloha Silver于2016年2月5日创作。
//版权所有©2016美国广播公司。版权所有。
//
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,强)UISearchController*searchController;
@属性(非原子,强)UITableView*tableView;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
[自设置表视图];
[自设置搜索界面];
[自我约束];
self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedlayoutincludesopaquebar=是;
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:动画];
[self.searchController.searchBar sizeToFit];
}
-(无效)setupTableView{
self.tableView=[[UITableView alloc]initWithFrame:CGRectZero-style:UITableView-stylegrouped];
self.tableView.dataSource=self;
self.tableView.delegate=self;
self.tableView.translatesAutoresizingMaskIntoConstraints=否;
[self.view addSubview:self.tableView];
}
-(无效)setupSearchInterface{
self.definesPresentationContext=是;
self.searchController=[[UISearchController alloc]initwithsearchresultscocontroller:nil];
self.searchController.dimsBackgroundDuringPresentation=否;
self.searchController.hidesNavigationBarDuringPresentation=否;
self.searchController.searchBar.scopeButtonTitles=@[@“一”,@“二”;
self.searchController.searchBar.translatesAutoResizezingMaskintoConstraints=否;
self.searchController.searchResultsUpdater=self;
[self.view addSubview:self.searchController.searchBar];
}
-(无效)设置约束{
NSDictionary*layoutViews=@{@“searchBar”:self.searchController.searchBar,@“tableView”:self.tableView,@“topLayoutGuide”:self.topLayoutGuide};
[self.view addConstraints:[nsLayoutConstraintsWithVisualFormat:@“H:|[searchBar]|”选项:0指标:无视图:LayoutView];
[self.view addConstraints:[nsLayoutConstraintsWithVisualFormat:@“H:|[tableView]|”选项:0指标:无视图:LayoutView];
[self.view addConstraints:[nsLayoutConstraintsWithVisualFormat:@“V:|[searchBar(44)][tableView]|“选项:0度量:无视图:LayoutView];
}
-(void)updateSearchResultsForSearchController:(UISearchController*)searchController{
NSLog(@“更新应在此处进行”);
}
#pragma标记-UITableViewDataSource
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回100;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellID=@“CellID”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellID];
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellID];
}
cell.textLabel.text=[NSString stringWithFormat:@“单元格编号%ld”,(长)indexPath.row];
返回单元;
}
@结束
它嵌入在
UINavigationController
实例中,最初按预期运行,如以下屏幕截图所示:

搜索栏
被激活时会出现问题。它似乎从屏幕上消失,但仔细检查视图后,我们确定它实际上在屏幕上,但宽度为零。下面的图片显示了此时呈现的内容:

我对自动布局没有太多经验,所以我一直认为我的约束肯定有问题,尽管在激活
UISearchController
时我不会弄乱它们


有什么办法可以让它工作吗?

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

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


抱歉,我不确定此占位符视图将如何处理范围按钮的大小更改。希望您能找出在更改后如何使该零件使用自动布局。

您在此处搜索过“UISearchBar消失”吗?这个问题被问了很多。其中一个答案可能适合你的情况。我已经找到了,不幸的是还没有找到任何能完全描述我所经历的事情。已经足够接近了,但是没有涉及自动布局(我已经通过手动设置框架使其工作,但我确实试图更好地理解自动布局a)。改为在这个占位符上设置约束。这就是我错过的!非常感谢,戴夫。我以前曾尝试过使用占位符视图,但我只是没有得到正确的约束。成功了!仅供参考,使用这种方法,
tableView
会自动下降,并在旁边设置动画