2
我需要爲應用程序實現多個視圖,其中內容基於UITableView
,該視圖應具有朝向父級的左/右/上邊距(UIView
)。更優雅的方式來實現UITableView周圍的利潤率?
目前我必須在故事板中手動創建約束。有沒有辦法通過編程實現它?
我需要爲應用程序實現多個視圖,其中內容基於UITableView
,該視圖應具有朝向父級的左/右/上邊距(UIView
)。更優雅的方式來實現UITableView周圍的利潤率?
目前我必須在故事板中手動創建約束。有沒有辦法通過編程實現它?
怎麼樣......
@interface CustomViewController()
@property (strong, nonatomic) UITableView *tableView;
@end
@implementation CustomTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [UITableView new];
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
self.tableView.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:self.tableView];
NSDictionary *views = @{@"topLayoutGuide": self.topLayoutGuide, @"tableView": self.tableView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[tableView]-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide]-20-[tableView]-20-|" options:0 metrics:nil views:views]];
}
@end
對於垂直約束我會用插圖的內容。這可以防止在向上或向下滾動桌面視圖時單元格「神奇消失」。 –