gpt4 book ai didi

ios - 使用布局 anchor 将 UiTableView 固定到( View Controller ) View

转载 作者:行者123 更新时间:2023-11-29 00:56:43 25 4
gpt4 key购买 nike

我想将一个 TableView “固定”到它的容器,即 View Controller 的 View 。由于 UIView 没有 anchor 属性,我尝试使用 layoutMarginsGuide 来实现。

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

但这应该如何运作?

最佳答案

首先将约束更改为这样
self.tableView.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
self.tableView.rightAnchor.constraintEqualToAnchor(view.rightAnchor).active = true

如果问题仍然存在,请确保将 tableview 添加为 view 的 subview

view.addSubview(tableView)

关于ios - 使用布局 anchor 将 UiTableView 固定到( View Controller ) View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37556405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com