我试图使用编程约束在UIScrollView中添加UIView,但我认为由于约束缺失,我无法看到UIView。当我试图通过CGRectMake添加其工作正常时我也检查了下面的代码,在视图层次结构中有UIScrollview内的视图。
我没有尝试过。解决方案但没有取得任何成功
UIScrollView with iOS Auto Layout Constraints: Wrong size for subviews
这是我的代码
vendorDetailsScrollView = UIScrollView()
vendorDetailsScrollView!.translatesAutoresizingMaskIntoConstraints = false
vendorDetailsScrollView!.backgroundColor = UIColor.redColor()
self.view.addSubview(vendorDetailsScrollView!)
vendorSubView = UIView()
vendorSubView!.translatesAutoresizingMaskIntoConstraints = false
vendorSubView!.backgroundColor = UIColor.greenColor()
self.vendorDetailsScrollView!.addSubview(vendorSubView!)
viewDictionary!["vendorDetailsScrollView"] = vendorDetailsScrollView!
viewDictionary!["vendorSubView"] = vendorSubView!
metricDictionary!["navigationHeight"] = navigationHeight
metricDictionary!["vendorDetailsScrollViewHeight"] = self.view.frame.size.height - navigationHeight
metricDictionary!["vendorSubViewHeight"] = 100
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[vendorDetailsScrollView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metricDictionary!, views: viewDictionary!))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-navigationHeight-[vendorDetailsScrollView(vendorDetailsScrollViewHeight)]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metricDictionary!, views: viewDictionary!))
self.vendorDetailsScrollView!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[vendorSubView]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metricDictionary!, views: viewDictionary!))
self.vendorDetailsScrollView!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[vendorSubView(vendorSubViewHeight)]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metricDictionary!, views: viewDictionary!))
答案 0 :(得分:2)
您需要添加以下代码
self.vendorDetailsScrollView.addConstraint(NSLayoutConstraint(item: vendorSubView, attribute: .Width, relatedBy: .Equal, toItem: vendorDetailsScrollView, attribute: .Width, multiplier: 1.0, constant: 0))
而不是
self.vendorDetailsScrollView!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[vendorSubView]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metricDictionary!, views: viewDictionary!))
答案 1 :(得分:0)
在vendorDetailsScrollView中添加一个视图作为子视图,然后在子视图中添加所有子视图。