以编程方式对搜索栏和 UITableView 使用 AutoLayout

Using AutoLayout programmatically for search bar and UITableView(以编程方式对搜索栏和 UITableView 使用 AutoLayout)

本文介绍了以编程方式对搜索栏和 UITableView 使用 AutoLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个几乎全屏的容器视图,没有状态栏.

然后我创建了一个 UISearchController 和一个 UITableView.我正在使用 ios 9 并以编程方式做事.我遇到的错误是当触摸搜索栏时,范围选项在其下方打开,但表格视图没有正确向下滑动.我该如何解决这个问题?

我 viewDidLoad 中的代码是:

//搜索控制器self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];self.searchController.searchResultsUpdater = 自我;self.searchController.dimsBackgroundDuringPresentation = NO;self.searchController.searchBar.scopeButtonTitles = @[@"A", @"B", @"C"];self.searchController.searchBar.delegate = self;[self.searchController.searchBar setTranslatesAutoresizingMaskIntoConstraints:NO];//UITableViewself.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];self.tableView.translatesAutoresizingMaskIntoConstraints = NO;//容器视图定义为整个屏幕,除了顶部20个点的状态栏[containerView addSubview:self.searchController.searchBar];[containerView addSubview:self.tableView];//约束 - 这里可能有问题?//搜索栏约束NSDictionary *views2 = @{@"searchBar": self.searchController.searchBar};[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|"选项:0 指标:nil 视图:views2]];[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[searchBar]|"选项:0 指标:nil 视图:views2]];//表格视图约束NSDictionary *views = @{@"tableView": self.tableView};[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|"选项:0 指标:无意见:意见]];[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|"选项:0 指标:无意见:意见]];

我需要让搜索栏范围选项和表格视图一起正确滑动.这是如何以编程方式完成的?

解决方案

您想将 UITableView 的顶部附加到 顶部布局指南的底部.

顶部布局指南仅在使用 故事板时可供您使用.

使用 Interface Builder 的另一个理由.

<小时>

原因 1 = 你一开始就不会有这个错误,你将能够可视化你的约束,你可以用恰好 0 行代码来实现所有这些.p>

我绝对肯定,只要付出适当的努力,并得到 SO 社区的支持,您就可以在代码中完成所有工作.我只是发现 IB 不仅节省了设计时间,而且将调试工作转移到了操作系统上,并确保任何应用程序的使用寿命更长.故事板让您可以专注于使您的应用与我的应用不同的地方,而不是为布局故障而苦恼.

I have a container view that is nearly the full screen sans the status bar.

I then created a UISearchController and a UITableView. I am using ios 9 and doing things programmatically. The bug I am having is when the search bar is touched, the scope options open beneath it but the tableview doesn't slide down correctly. How do I fix this?

The code in my viewDidLoad is:

// Search controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = @[@"A", @"B", @"C"];
self.searchController.searchBar.delegate = self;
[self.searchController.searchBar setTranslatesAutoresizingMaskIntoConstraints:NO];

// UITableView
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;

// Container view is defined as the whole screen except 20 points at the top where the status bar is
[containerView addSubview:self.searchController.searchBar];
[containerView addSubview:self.tableView];

// The constraints - something is probably wrong here?

// Search bar constraints
NSDictionary *views2 = @{@"searchBar": self.searchController.searchBar};
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|" options:0 metrics:nil views:views2]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[searchBar]|" options:0 metrics:nil views:views2]];

// Tableview constraints
NSDictionary *views = @{@"tableView": self.tableView};
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:views]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:nil views:views]];

I need to make the search bar scope options and table view slide properly together. How is this done programmatically?

解决方案

You want to attach the top of the UITableView to the bottom of the top layout guide.

That top layout guide is only available to you when using a Storyboard.

One more reason to use Interface Builder.


Reason number 1 = you wouldn't have this bug in the first place, you would be able to visualize your constraints, and you could achieve all this with exactly 0 lines of code.

I'm absolutely certain that with the right amount of effort, and support from the SO Community, you can do everything in code. I just find that IB saves not only design time, but offloads debugging to the OS, and ensures longer lifespan to any app. Storyboard allows you to concentrate on what makes your app different from my app, instead of agonizing over layout glitches.

这篇关于以编程方式对搜索栏和 UITableView 使用 AutoLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:以编程方式对搜索栏和 UITableView 使用 AutoLayout

基础教程推荐