使用布局锚将UiTableView固定到(查看控制器)视图

时间:2016-05-31 22:06:55

标签: ios swift autolayout ios9 nslayoutanchor

我想把一个tableview'pin'到它的容器,一个视图控制器的视图。由于UIView没有锚属性,我尝试使用layoutMarginsGuide。

  

UIView不为布局边距属性提供锚属性。相反,layoutMarginsGuide属性提供了一个表示这些边距的UILayoutGuide对象。使用指南的锚属性来创建约束。

CALL_CONVENTION

但运行此代码我收到以下错误:

        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.'

但这应该如何运作?

0 个答案:

没有答案