用的UITableView自动版式和偏移的限制 [英] UITableView with autolayout and offset constraints
问题描述
我有以下的code以添加一个的UITableView
随偏移距的限制:
I have the following code to add a UITableView
with offset constraints:
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)
我不断收到以下错误:
I keep getting the following error:
(
"<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.
是不是我做错了吗?我试图basicaly获得的UITableView
与在顶部和底部偏移量。我读兴田此页面上的以下内容:
Is there something i'm doing wrong? I'm trying to basicaly get a UITableView
with an offset at top and bottom. I was reading hte following on this page:
https://medium.com/@jsleeuw/mastering-programmatic-自动布局b02ed2499d79
推荐答案
如果您编程方式创建视图,并使用自动布局,你需要通过调用<一个禁用默认约束的UIKit首先创建href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/setTranslatesAutoresizingMaskIntoConstraints:\"相对=nofollow> setTranslatesAutoresizingMaskIntoConstraints()
插入该视图进入前等级:
If you create views programatically and use auto layout, you need to disable the default constraints UIKit creates first by calling setTranslatesAutoresizingMaskIntoConstraints()
before you insert the view into the hierarchy:
self.tableView = UITableView()
self.tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(self.tableView)
这篇关于用的UITableView自动版式和偏移的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!