2016-04-06 9 views
0

Xamarin iOSを使用して、デザイナなしでC#コードを使用してビューを構築すると、異なるサイズのクラスに対して異なる制約セットを作成するにはどうすればよいですか?Xamarin iOSを使用して自動レイアウトに異なる制約を設定する方法

今のところ、私が管理しているのは、コードのみを使用して制約を作成することです。すべてのサイズ/向きに対して同じ制約が適用されます。

このコードでは、2つのカスタムビューを作成し、画面を左から右に塗りつぶし、それらをもう一方の下に配置します。これはiPhone 6+のポートレートモードには最適ですが、ランドスケープモードでは、2番目のビュー(SomePanel)を非表示にし、最初のビュー(MainPanel)を画面全体に伸ばしたいと思っています。

partial class SomeView : MvxViewController 
{ 
    CustomView MainPanel; 
    CustomView SomePanel; 

    public SomeView (IntPtr handle) : base (handle) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     /// First Panel 
     MainPanel = CustomView.Create(); 
     View.AddSubview (MainPanel); 
     MainPanel.TranslatesAutoresizingMaskIntoConstraints = false; 


     /// Second Panel 
     SomePanel = CustomView.Create(); 
     View.AddSubview (SomePanel); 
     SomePanel.TranslatesAutoresizingMaskIntoConstraints = false; 



     /// <summary> 
     /// First set of constraints - two panels one under the other 
     /// </summary> 
     View.AddConstraint (NSLayoutConstraint.Create (MainPanel, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1f, 0f)); 
     View.AddConstraint (NSLayoutConstraint.Create (MainPanel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1f, 50f)); 
     View.AddConstraint (NSLayoutConstraint.Create (MainPanel, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 60f)); 
     View.AddConstraint (NSLayoutConstraint.Create (MainPanel, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, View.Bounds.Width - 40)); 

     View.AddConstraint (NSLayoutConstraint.Create (SomePanel, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1f, 0f)); 
     View.AddConstraint (NSLayoutConstraint.Create (SomePanel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, MainPanel, NSLayoutAttribute.Bottom, 1f, 20f)); 
     View.AddConstraint (NSLayoutConstraint.Create (SomePanel, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 60f)); 
     View.AddConstraint (NSLayoutConstraint.Create (SomePanel, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, View.Bounds.Width - 40)); 

    } 

} 

私はデザイナーを使用して行う、あなたの制約を設定し、デザイナーを使用してサイズのクラスを変更して、デザイナーから再び、制約を編集することは容易である知っています。

私の場合、私はコードを使ってそれをやりたいのですが、私は実際には異なるサイズのクラス/異なる方向に対して異なるセットの制約を設定する方法のガイドや例を見つけることができません。

何か助けていただければ幸いです。

+0

オリエンテーションが変更されたときにその変更を無効にする方法がありますuldあなたの意見を設定します。 – PmanAce

答えて

1

あなたは、ちょうどここに表示制約

public override void WillAnimateRotation (UIInterfaceOrientation toInterfaceOrientation, double duration) 
{ 
    base.WillAnimateRotation (toInterfaceOrientation, duration); 
    //Update view constraints... 
} 

通常、これはのViewControllerがロードされたときに発生する、UpdateViewConstraints上のすべてのビューを管理している別のオプションを更新して変化の方向を検出するためのWillAnimateRotationをオーバーライドすることもできますが、我々はそれを発射でき毎回向きが変わるが更新するたびにWillAnimateRotation

public override void WillAnimateRotation (UIInterfaceOrientation toInterfaceOrientation, double duration) 
    { 
     base.WillAnimateRotation (toInterfaceOrientation, duration); 
     View.SetNeedsUpdateConstraints(); 
    } 

    public override void UpdateViewConstraints() 
    { 
     base.UpdateViewConstraints(); 
     var currentOrientation = InterfaceOrientation; 
     //Update view constraints with current orientation 
    } 
1

これは、画面の幅/高さ/方向を確認し、それに応じて制約を手動で更新することで手動で行う必要があります。

ストーリーボードを使用する場合は、iOSがそうです。

Btwでは、コードに制約を追加する簡単な方法として、石工のナゲットや流暢なナゲットを使用することができます。

関連する問題