【问题标题】:UITableView with autolayout and offset constraints具有自动布局和偏移约束的 UITableView
【发布时间】:2014-08-25 22:36:18
【问题描述】:

我有以下代码来添加带有偏移约束的UITableView

self.tableView = UITableView()
self.view.addSubview(self.tableView)

let views: NSDictionary = ["tableView": self.tableView]
let horizontal: NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|[tableView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: views)
let vertical: NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|[tableView]|", options: NSLayoutFormatOptions(0), metrics: ["offsetTop": 150, "offsetBottom": 49], views: views)

var constraints: NSMutableArray = NSMutableArray()
constraints.addObjectsFromArray(horizontal)
constraints.addObjectsFromArray(vertical)
self.view.addConstraints(constraints)

我不断收到以下错误:

(
    "<NSLayoutConstraint:0x17e8d9f0 V:|-(0)-[UITableView:0x18893a00]   (Names: '|':UIView:0x17d7da90 )>",
    "<NSLayoutConstraint:0x17e8da50 V:[UITableView:0x18893a00]-(0)-|   (Names: '|':UIView:0x17d7da90 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x17d886e0 h=--& v=--& V:[UITableView:0x18893a00(0)]>",
    "<NSLayoutConstraint:0x17d88bd0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x17d7da90(568)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17e8da50 V:[UITableView:0x18893a00]-(0)-|   (Names: '|':UIView:0x17d7da90 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我做错了什么吗?我试图基本上得到一个UITableView,在顶部和底部都有一个偏移量。我正在阅读此页面上的以下内容:

https://medium.com/@jsleeuw/mastering-programmatic-auto-layout-b02ed2499d79

【问题讨论】:

    标签: uitableview swift autolayout


    【解决方案1】:

    如果您以编程方式创建视图并使用自动布局,则需要先通过调用setTranslatesAutoresizingMaskIntoConstraints() 禁用 UIKit 创建的默认约束,然后再将视图插入层次结构:

    self.tableView = UITableView()
    self.tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(self.tableView)
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2019-07-22
      • 2016-02-17
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多