使用自动版式为编程搜索栏的UITableView [英] Using AutoLayout programmatically for search bar and UITableView

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

问题描述

我有一个容器认为是几乎全屏幕SANS的状态栏。

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

然后,我创建了一个UISearchController和一个UITableView。我使用的是iOS 9和编程做的事情。我遇到的bug是当搜索栏被触摸时,范围选项它下面打开,但tableview中没有正确滑下。我该如何解决这个问题?

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?

在我viewDidLoad中的code是:

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?

推荐答案

您想要的的UITableView 顶部连接到顶级布局导向的底部

这是最常见的布局指南的使用时,只提供给你的故事板

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

一个理由使用Interface Builder。

One more reason to use Interface Builder.

理由号1 =你就不必首先这个bug,你就能够想象你的限制,你可以实现这一切与code正好0线。

我绝对肯定,随着精力适量,并从SO社区的支持,你可以做code一切。我刚刚发现的 IB 不仅保存设计时间,但卸载调试到操作系统,并确保更长的寿命到任何应用程序。 故事板使您可以通过布局毛刺专注于什么使您的应用程序不同,我的应用程序,而不是痛苦。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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