我需要顶级约束的未知元素
我有一个应用程序通过单击按钮从服务器下载图像。图像下载后,我创建一个新的imageView并将其添加到我的contentView(UIView)。我需要创建的限制 - 每一个新的ImageView需要从以前的一个我需要顶级约束的未知元素
func addNewImageToTheScrollView(img: UIImage?) {
if let imageResponse = img {
let imageView = UIImageView(image: imageResponse.crop(rect: CGRect(x: 0, y: imageResponse.size.height/2, width: self.contentView.frame.width, height: 200)))
self.contentView.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
let x = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1.0, constant: 0)
let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
let width = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1.0, constant: 0)
let height = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150)
self.contentView.addConstraints([x, y])
imageView.addConstraints([width, height])
}
}
顶部约束。如果我评论约束代码,这将是做工精细,除非每一个新的ImageView将在同一个地方,在顶部风景。现在下载后白衣这个约束代码,我有这样的代码问题
2017年7月2日14:50:01.018 ImageFromServerTest [11516:1080948 ***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:' NSLayoutConstraint for>:乘数为零或零的第二个项目与第一个属性的位置一起创建位置等于常量的非法约束。位置属性必须成对指定。'
每当你与scrollViews工作,有2条拇指规则是: -
-
给了滚动前置,底部和顶部约束相对于所述上海华,即
self.view
@IbOutlet weak var scrollView: UIScrollView! let leading = NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1.0, constant: 0) let trailing = NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1.0, constant: 0) let top = NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 0) let bottom = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0)
-
相对于添加的限制的内容查看到滚动视图
@IbOutlet weak var contentView: UIView! let leading = NSLayoutConstraint(item: contentView, attribute: .leading, relatedBy: .equal, toItem: scrollView, attribute: .leading, multiplier: 1.0, constant: 0) let trailing = NSLayoutConstraint(item: contentView, attribute: .trailing, relatedBy: .equal, toItem: scrollView, attribute: .trailing, multiplier: 1.0, constant: 0) let top = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1.0, constant: 0) //increase the constant according to how much long you need the scrollview to be let bottom = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 0)
现在对于添加子视图的限制(标签,图像)的内容查看
对于示例 - 您收到您的第一形象,因此我们将保持的UIImageViews你的函数之外的数组。
var imageViews = [UIImageViews]() //declared outside the function
//received an image here
var imageView = UIImageView() // define the frame according to yourself using frame init method
imageView.image = image
if imageViews.isEmpty { // first image view
//add constraints to first image view
let x = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: contentView, attribute: .centerX, multiplier: 1.0, constant: 0)
let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: 30)
let width = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1.0, constant: 0)
let height = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150)
}
else { //second, third, fourth image view... so on
let x = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: contentView, attribute: .centerX, multiplier: 1.0, constant: 0)
let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: imageViews[imageViews.count - 1], attribute: .bottom, multiplier: 1.0, constant: 30)
let width = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1.0, constant: 0)
let height = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150)
}
imageViews.append(imageView)
}
希望你现在有了一个想法,该如何着手解决这个问题。如果具有超过4或5个图像视图,则可能需要检查数组的计数并相应地增加scrollView的contentView。您可以通过使用 self.scrollView.contentSize = CGSize(width, height)
我相信你在这里有一个问题:
let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
“toItem” 参数应该有一定的价值。另外第一个参数应该是imageView.topAnchor。也许这应该是这个样子:
let y = NSLayoutConstraint(item: imageView.topAnchor, attribute: .top, relatedBy: .equal, toItem: self.contentView.topAnchor, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
Up vote。我有一个评论准备发表一些这样的说法,但决定它更多的是一个答案。更多的是(从OP中隐含的其他东西)比我提供的“答案” - 所以我只是没有发布任何东西。很好的解释在UIScrollView中正确设置东西的各种部分 – dfd
谢谢.. ....:) –
你是伟大的,谢谢))我认为这将是我很多时间了解是否有其他限制我自己)) – sergs