2017-11-04 14 views
1

ここでは、アクティビティインジケータをプログラムで使用し、プログラムで制約を与えましたが、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() 
} 
+0

あなたのコードで私にとって間違っていることは、制約を作成することですが、実際にはそれらをいくつかのビューに割り当てることはありません。 –

+0

@WolfgangSchreurs、制約がアクティブになるので、iOSはそれらを適切なビューに追加します。 – vacawama

答えて

1

あなたが制約を作成する前に、あなたのサブビューを追加します。

移動し、この行:

contentView.addSubview(container) 

制約の作成前/活性化に。

+0

今それはクラッシュしないが、それは中心にそれを表示する方法を画面から外ですか? – user0246

+0

さらに制約が必要です。 'container.translatesAutoresizingMaskIntoConstraints = false'を設定すると、' frame'は使用されません。 'container'ビューの' width'と 'height'を設定してください。 – vacawama

+0

私は幅と高さも指定しましたが、画面の横に横たわっています – user0246

関連する問題