2016-05-31 78 views
0

我想'將'tableview視圖到它的容器,視圖控制器的視圖。由於UIView沒有錨點屬性,我試着用layoutMarginsGuide來做到這一點。將UiTableView綁定到(視圖控制器)與佈局定位點的視圖

UIView does not provide anchor properties for the layout margin attributes. Instead, the layoutMarginsGuide property provides a UILayoutGuide object that represents these margins. Use the guide’s anchor properties to create your constraints.

 let margins = self.view.layoutMarginsGuide 
    self.tableView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true 
    self.tableView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor).active = true 

但運行此代碼我收到以下錯誤:

** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with items <UITableView: 0x7fdfd0ab5e00; frame = (0 0; 768 1024); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fdfd8452e00>; layer = <CALayer: 0x7fdfd84c11c0>; contentOffset: {0, 0}; contentSize: {0, 0}> and <UILayoutGuide: 0x7fdfd86d7ca0 - "UIViewLayoutMarginsGuide", layoutFrame = {{0, 0}, {0, 0}}, owningView = <UIView: 0x7fdfd75a8aa0; frame = (0 0; 768 1024); autoresize = W+H; layer = <CALayer: 0x7fdfd0409270>>> because they have no common ancestor. Does the constraint reference items in different view hierarchies? That's illegal.' 

的重要組成部分:

...because they have no common ancestor. Does the constraint reference items in different view hierarchies? That's illegal.' 

但是,這應該是怎樣工作的?

+0

沒有這樣的問題在iOS模擬器9子視圖。 – BangOperator

回答

1

第一個變化約束是這樣 self.tableView.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true self.tableView.rightAnchor.constraintEqualToAnchor(view.rightAnchor).active = true

如果問題仍然存在,確保實現代碼如下新增爲圖

view.addSubview(tableView) 
+0

非常感謝!我確實已經添加了tableView _after_分配約束 – brainray

相關問題