如何使用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));
}
}
我知道這是很容易使用設計從設計師做,設置您的約束條件,改變使用設計規模類和編輯的約束,再次。
但在我的情況下,我想要使用代碼來做到這一點,我不能真正找到一個指導或如何爲不同的大小類別/不同的方向設置不同的約束集合的例子。
任何幫助表示讚賞。
有一種方法,你可以重寫,當方向改變時觸發,在那裏你和我uld設定你的觀點。 – PmanAce