在这里,我以编程方式使用了活动指示器并以编程方式给出了约束,但在NSLayoutConstraint
崩溃时返回错误,任何人都可以帮我解决这个问题吗?
这是我在控制台中的错误
2017-11-04 16:46:41.657电子商务[2744:71044] ***终止应用程序到期 未被捕获的异常' NSGenericException',原因:'无法 用锚点激活约束 因为他们没有共同的祖先。约束或其约束 锚点引用不同视图层次结构中的项目?那' S 。非法'
func showActivityIndicator(uiView: UIView) {
container.frame = uiView.frame
container.center = uiView.center
container.backgroundColor = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3)
loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
loadingView.center = uiView.center
loadingView.backgroundColor = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
customActivityIndicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
customActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
customActivityIndicator.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2)
loadingView.addSubview(customActivityIndicator)
view.isUserInteractionEnabled = false
container.addSubview(loadingView)
container.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(
item: container,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1.0,
constant: 0.0
).isActive = true
NSLayoutConstraint(
item: container,
attribute: .centerY,
relatedBy: .equal,
toItem: view,
attribute: .centerY,
multiplier: 1.0,
constant: 0.0
).isActive = true
NSLayoutConstraint(item: container, attribute: .width, relatedBy: .equal, toItem: contentView, attribute: .width, multiplier: 1, constant: view.frame.size.width / 2).isActive = true
NSLayoutConstraint(item: container, attribute: .height, relatedBy: .equal, toItem: contentView, attribute: .height, multiplier: 1, constant: view.frame.size.height / 2).isActive = true
contentView.addSubview(container)
customActivityIndicator.startAnimating()
}
答案 0 :(得分:1)
在创建约束之前添加子视图。
移动此行:
contentView.addSubview(container)
在创建/激活约束之前。