objective-c - Interface Builder "locking"对象到屏幕边缘

标签 objective-c xcode interface-builder autolayout

我已经阅读了一些关于使用 AutoLayout 的教程,但我似乎无法弄清楚如何实现我认为应该非常简单的东西。我正在为 3.5 英寸和 4 英寸 iPhone/iPod Touch 屏幕设计一个应用程序。这是一个简单的标签栏应用程序,UITableView 填充了每个标签的全部内容,如下所示:

Interface Builder

我希望 UITableView 锁定到屏幕的边缘,无论屏幕是 3.5 英寸还是 4 英寸。我目前使用的在 4 英寸屏幕上运行良好,但在 3.5 英寸屏幕上,UITableView 超出了屏幕宽度。

我曾尝试阅读一些 AutoLayout 教程以及摆弄 Interface Builder 约束,但没有成功。如果您能提供任何帮助,我将不胜感激。

最佳答案

您需要将 UITableView 附加到父 UIView 的所有边缘,然后 UITableView 将扩展或收缩以填充 UIView 在设备上。这将使它在所有 iDevices(包括 iPad)上看起来都是合适的尺寸。如下图所示,您只需点击所有红色虚线引用线,确保边距设置为 0(触摸两侧):

AutoLayout via IB for a UITableView touching all sides of the parent view

您还可以在 UITableViewCtrl + 向左拖动、向右拖动、向上拖动和向下拖动,选择“Leading Space to Container”、“Trailing Space to Container” ”、“顶部空间到顶部布局指南”和“底部空间到底部布局”。

或者,您可以使用 Visual Format Language (VFL) (下面的 UIViewController 代码假定您的 UITableView 是一个名为 tableView 的自动@synthesized @property ):

/* Turn off springs/structs */
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;

/* Leading and trailing constraints */
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)]];
/* Top and bottom constraints */
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)]];

...或者如果您真的喜欢直截了当(并​​且喜欢打字):

/* Leading constaint (could use NSLayoutAttributeLeft here as well) */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
/* Trailing constraint (could use NSLayoutAttributeRight here as well) */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
/* Top constraint */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
/* Bottom constraint */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

所有这一切的重要之处在于 UITableViewUIViewControllerview 的子级(很可能是) .默认情况下,view 将按预期填充屏幕,并且使用上述所有方法,您要求布局紧贴最大化 View 的边缘。

关于objective-c - Interface Builder "locking"对象到屏幕边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20980985/

相关文章:

xcode - xcode 6 zoomToRect动画问题

ios - 如何在 iOS 的 HealthKit 中保存 HKQuantityTypeIdentifierBodyMass 类型的样本

iphone - 视网膜显示屏 iphone 的图像

objective-c - 重命名连接的 Action

xcode - 是否可以在不使用 Interface Builder 的情况下创建 Mac 应用程序..?

iphone - 如何在 ios 项目中显示 GADInterstitial?

ios - 为什么我的 tableview 只按升序显示日期而不显示其余数据

objective-c - UILabel字体大小?

ios - 相应地移动 View View 折叠/展开动画

ios - 如何将 CATransform3DTranslate 转换为 CGAffineTransform,以便它可以模仿轮播 View