斯威夫特3 |以编程方式添加约束并不会使我的子视图

时间:2017-01-17 09:37:05

标签: uiimageview swift3 constraints center

我尝试编写一个UIImageView扩展,在我的ImageView中间显示一个ActivityIndi​​cator,我尝试了以下操作,请注意我的ImageViews对高度(100)和宽度(100)有约束:

public extension UIImageView {
    public func downloadedFrom(url: URL) {

        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
    activityIndicator.startAnimating()

        let leftSpaceConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 40)

        let topSpaceConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 40)

        let widthConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)

        let heightConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)

        self.addSubview(activityIndicator)

        self.addConstraints([leftSpaceConstraint, topSpaceConstraint, widthConstraint, heightConstraint])
    }
}

我也尝试设置leadingSpaceConstraint / trailingSpaceConstraint / horizo​​ntalConstraint / verticalConstraint但我的代码没有效果,我设置的每个约束都没有改变任何东西。我的activityIndi​​cator就像这样留在左上角:

enter image description here

我不明白为什么?方法"已下载来自"在viewDidLoad中调用如下:

myImageView.downloadedFrom(url: imageUrl)

我哪里出错了? TY

1 个答案:

答案 0 :(得分:1)

您将imageView放在左侧的父UIView顶部。它需要依赖于父UIImageView中间坐标的x轴和y轴。而且,将translatesAutoresizingMaskIntoConstraints设置为false。请看下面的代码:

     public extension UIImageView {
            public func downloadedFrom(url: URL) {

            let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
            activityIndicator.startAnimating()
            activityIndicator.translatesAutoresizingMaskIntoConstraints = false

            let leftSpaceConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)

            let topSpaceConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)

            let widthConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)

            let heightConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)

            self.addSubview(activityIndicator)

            self.addConstraints([leftSpaceConstraint, topSpaceConstraint, widthConstraint, heightConstraint])
            }
        }