2017-06-06 5 views
1

仕事と私はviewDidAppearにこのコードを呼び出すこのXIB、中央に高さを修正 - プログラム的制約は、私は自由形式で.xibを作成

if let alertView = Bundle.main.loadNibNamed(Constants.XIB.titleImageLabelThreeButtonsAlertView, owner: self, options: nil)?.first as? TitleImageLabelThreeButtonsAlertView { 

     view.addSubview(alertView) 

     alertView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY) 

     view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 20)) 
     view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 20)) 

     alertView.autoresizingMask = [UIViewAutoresizing.flexibleLeftMargin, UIViewAutoresizing.flexibleRightMargin, UIViewAutoresizing.flexibleTopMargin, UIViewAutoresizing.flexibleBottomMargin] 
     self.view.layoutIfNeeded() 

    } 

のようにそれを実装していないようです。中心的なことはうまくいくようですが、trailingleadingは効果がないようです。私は彼らの距離を20にしたい、私のalertViewは、固定された高さとセンターに表示する必要があります。

XIBが常に同じ大きさ(スクリーンショットを参照)

私が最初に目標としているが、私はすべてのデバイスのためのすべてのビューに実装することができますXIBを取得することでした。だからこれを得るための最善の方法は何ですか?

xib file 私XIBファイル iphone 7 シミュレータiphone 7 iphone 4 シミュレータiphone 4

+2

私はオートレイアウトと自動サイズ変更のマスクを混ぜるのは良いアプローチではないと思います。これらは、衝突して未定義の結果を生む2つの異なるメカニズムです。 –

+0

お返事ありがとうございます。私は何をしたいのかを知る方法を知っていますか? – kuzdu

+1

自動サイズ変更マスクを設定するのではなく、 'subview.translatesAutoresizingMaskIntoConstraints = false'をキャンセルしてください。さらに、垂直方向の拘束(高さまたはyオフセット)を設定しないと、不適切な結果が生じる可能性があります。 –

答えて

1

あなたは、自動レイアウトと(自動サイズ変更マスク付き)固定配置を取り違えています。あなたがしたいことは、レイアウトが自動的にレイアウトを調整するように、自動レイアウトを完全に使用することです。あなたは固定の高さ、あなたが20の水平方向の距離をしたいと言うと、私はこれを行うだろうように、中央に配置する:

if let alertView = Bundle.main.loadNibNamed(Constants.XIB.titleImageLabelThreeButtonsAlertView, owner: self, options: nil)?.first as? TitleImageLabelThreeButtonsAlertView { 

    view.addSubview(alertView) 

    // Start using auto layout 
    alertView.translatesAutoresizingMaskIntoConstraints = false 

    // Set the leading and trailing constraints for horizontal placement 
    view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -20)) 
    view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 20)) 

    // Centre it vertically 
    view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)) 

    // Set the fixed height constraint 
    let fixedHeight: CGFloat = 100 
    view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 0, constant: fixedHeight)) 
} 

あなたがどんなにデバイス、スーパー、方向等の変更したい何を取得すること。

関連する問題