我有一个scrollview,我在循环中以编程方式创建和添加tableviews,同时在滚动视图中添加每个表,我也调用我的函数'addConstraintsOnTblView'来添加约束。 仅当您在该方向打开它并且不更改它时,这适用于两个方向。加载并更改方向后,滚动视图的滚动视图将在故事板中按预期正确应用,但表视图方向不会相应更新。 请让我知道什么是错的。 我尝试在viewWillTransitionToSize中应用tblsScrollView.updateConstraints()但它没有更新。
这是我的代码
func createTblViews {
for i in 0..<array.count {
let tblView = UITableView(frame: CGRect(x: 0, y: 0, width: 382, height: Int(self.tblsScrollView.frame.height)))
tblView.registerNib(UINib(nibName: "DiskDataTableViewCell", bundle: nil), forCellReuseIdentifier: "drivedatacell")
tblView.backgroundColor = UIColor.clearColor()
tblView.showsVerticalScrollIndicator = false
tblView.separatorStyle = .None
tblsScrollView.addSubview(tblView)
self.addConstraintsOnTblView(tblView, xConstraintVal: CGFloat(i * 382)) // 382 is width of each table
self.tblsScrollView.setNeedsUpdateConstraints()
}
}
func addConstraintsOnTblView(tblView: UIView, xConstraintVal: CGFloat) {
let leadConstraint = NSLayoutConstraint(item: tblView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: tblsScrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: xConstraintVal)
let topConstraint = NSLayoutConstraint(item: tblView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tblsScrollView, attribute: NSLayoutAttribute.Top , multiplier: 1.0, constant: 0)
let widthConstraint = NSLayoutConstraint(item: tblView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 382) // 382 is width of each table
let bottomConstraint = NSLayoutConstraint(item: tblView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: tblsScrollView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
tblsScrollView.translatesAutoresizingMaskIntoConstraints = false
tblsScrollView.addConstraints([leadConstraint, topConstraint, widthConstraint, bottomConstraint])
}