2016-04-06 28 views
0

如何使用Xamarin iOS在C#代碼背後構建我的視圖(沒有設計器),我如何爲不同的大小類創建不同的約束集?如何使用後臺代碼爲使用Xamarin iOS的自動佈局設置不同的約束

現在,我管理的所有東西都是僅使用代碼創建約束,並且它工作得很好,但它對於所有大小/方向都是同一組約束。

此代碼創建兩個自定義視圖,並將它們設置爲從左到右填充屏幕,並將它們定位在另一個之下。這對於縱向模式下的iPhone 6+非常適用,但在橫向模式下,我希望它隱藏第二個視圖(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

您可以覆蓋WillAnimateRotation檢測方向改變,只是更新在這裏查看約束

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

另一個選項是管理上UpdateViewConstraints所有視圖,通常這是在視圖控制器被加載解僱,但我們可以火了再次每次方向改變但更新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在使用故事板時爲您做到了這一點。

順便說一下,你可以使用砌體nuget或fluentlayout nuget更簡單的方法來添加代碼約束。

相關問題