ここでは、アクティビティインジケータをプログラムで使用し、プログラムで制約を与えましたが、NSLayoutConstraint
でクラッシュしました。エラーを解決する方法は誰にもわかります。 46::ここSwift 3で自動レイアウトの問題を解決するには?
は、コンソール
2017年11月4日16の私の誤りである41.657 eコマース[2744:71044] ***キャッチされない例外に による終了アプリ 'NSGenericException'、理由:「できません。 アンカーで拘束を有効にし、共通の祖先がないため に制約を適用します。制約またはその アンカーは、異なるビュー階層の項目を参照していますか?それは です。
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()
}
あなたのコードで私にとって間違っていることは、制約を作成することですが、実際にはそれらをいくつかのビューに割り当てることはありません。 –
@WolfgangSchreurs、制約がアクティブになるので、iOSはそれらを適切なビューに追加します。 – vacawama