斯威夫特 3 |以编程方式添加约束不会使我的子视图居中
Posted
技术标签:
【中文标题】斯威夫特 3 |以编程方式添加约束不会使我的子视图居中【英文标题】:Swift 3 | Adding constraints programmatically doesn't center my subview 【发布时间】:2017-01-17 09:37:04 【问题描述】:我尝试编写一个 UIImageView 扩展,它在我的 ImageView 中间显示一个 ActivityIndicator,我尝试了以下操作,请注意我的 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/horizontalConstraint/verticalConstraint,但我的代码没有效果,我设置的每个约束都不会改变任何东西。我的 activityIndicator 像这样停留在左上角:
我不明白为什么?在 viewDidLoad 中调用方法“downloadedFrom”,如下所示:
myImageView.downloadedFrom(url: imageUrl)
我哪里做错了? TY
【问题讨论】:
【参考方案1】:您将 imageView 从左侧放置在父 UIView 的顶部。它需要取决于 x 轴和 y 轴的父 UIImageView 中间的坐标。此外,将 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])
【讨论】:
感谢您的回答,您是对的,我应该使用 NSLayoutAttribute.centerX 和 NSLayoutAttribute.centerY 但这并没有改变任何东西,我的加载器仍在左上角我找不到居中的方法它! @Aximem activityIndicator.translatesAutoresizingMaskIntoConstraints = false ? 是的!谢谢@dasha,你可以用这个创建一个新的答案,我会接受;) 我曾经使用过 SnapKit,你只需要这样做:activityIndicator.snp.makeConstraints (make) -> Void in make.width.height.equalTo(20) make.center.equalTo(self)
它会自动管理 translatesAutoresizingMaskIntoConstraints以上是关于斯威夫特 3 |以编程方式添加约束不会使我的子视图居中的主要内容,如果未能解决你的问题,请参考以下文章