objective-c - 如何使用自动布局以编程方式创建 UITableView?

标签 objective-c cocoa-touch uitableview uiview autolayout

它似乎假设您使用框架而不是自动布局,因为您只能使用框架进行真正的初始化。我这样做错了吗?

最佳答案

您可以通过编程方式使用自动布局。 您不需要使用框架初始化 UITableView。相反,您必须设置translatesAutoresizingMaskIntoConstraints = NO,将 TableView 添加到父 View ,然后在代码中定义约束。 例如:

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
NSDictionary *views =
      NSDictionaryOfVariableBindings(tableView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
                               @"H:|[tableView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
                               @"V:|[tableView]|" options:0 metrics:nil views:views]];

请引用https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AdoptingAutoLayout/AdoptingAutoLayout.html了解有关视觉格式的更多信息。您还可以选择不使用视觉格式并以编程方式描述约束。

关于objective-c - 如何使用自动布局以编程方式创建 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26837201/

相关文章:

objective-c - 如何在 iPad 上分两列显示文本

ios - 从 Storyboard加载的 View Controller 应该强还是弱?

iphone - 一个部分中的一个部分 - UITableView -

objective-c - 从 UITableViewCell 中的 DisclosureButton 呈现 UIPopover

iphone - NSMutableArray removeObjectAtIndex

ios - iOS 5和iOS 6中UITabbar的区别

objective-c - subview 中的 NSView TouchesMovedWithEvent 正在暂停带有内置触控板的 SKScene,但不是魔术触控板?

objective-c - 将 iPhone 模拟器上的核心数据记录加载到 iPhone 4 的巨大速度差异

iphone - UITableView 单元格之间的空间

ios - XCode 6.3 向 tableviewcell 添加边距