[英]Xcode Auto Layout Vertical Constraints on Table View with Multiple Dynamic Heights
[英]Auto layout constraints for dynamic view
我正在為IOS(xamarin.iOS)創建自定義uicontrol。 uicontrol由文本字段和按鈕組成。 我通過擴展UIView創建了自定義控件
復雜性
文本字段的寬度將由用戶指定,並且需要在運行時呈現。
場景:將在應用程序中為用戶提供一個表格,用戶可以在其中輸入控件的寬度,例如,如果用戶輸入50並單擊了“提交”按鈕,則在下一頁呈現自定義UIcontrol時只需要花費50%屏幕總寬度的百分比。
問題
我需要為文本字段應用自動布局約束。 並且我添加了約束,只有最上面和最左邊的約束都按預期工作。 當我旋轉設備時,右邊距沒有按比例變化
this.AddConstraint (
NSLayoutConstraint.Create(textField,NSLayoutAttribute.Left,NSLayoutRelation.Equal,this, NSLayoutAttribute.Left, 1 , 10)
);
this.AddConstraint (
NSLayoutConstraint.Create(textField,NSLayoutAttribute.Right,NSLayoutRelation.Equal,this, NSLayoutAttribute.Left, 1 , ((UIScreen.MainScreen.Bounds.Width * editTextWidth)/100)+10)
);
this.AddConstraint (
NSLayoutConstraint.Create( textField, NSLayoutAttribute.Top, NSLayoutRelation.Equal,label, NSLayoutAttribute.Top, 1, 30+10)
);
this.AddConstraint (
NSLayoutConstraint.Create(textField, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, 3 * 10)
);
我不知道您如何在xamarin中做到這一點,但我可以解釋如何做到這一點。
我假設您的起始x和y坐標保持不變。
然后可以添加到開始的兩個約束是:
NSLayoutConstraint.Create(textField,NSLayoutAttribute.Left,NSLayoutRelation.Equal,this, NSLayoutAttribute.Left, 1 , 10)
NSLayoutConstraint.Create( textField, NSLayoutAttribute.Top, NSLayoutRelation.Equal,label, NSLayoutAttribute.Top, 1, 30+10)
您應該添加的其他兩個約束是文本字段的恆定高度和寬度約束。
當視圖加載時,只需更改數組中相同約束的常量即可,您可以通過執行以下操作輕松獲得:
constraintsArray = textfield.constraints;
這將在最初設置您的約束。
現在,設置視圖以偵聽設備方向的更改並再次更新常量:
NSNotificationCenter.DefaultCenter
.AddObserver("UIDeviceOrientationDidChangeNotification", DeviceRotated );
private void DeviceRotated(NSNotification notification){
switch (UIDevice.CurrentDevice.Orientation){
case UIDeviceOrientation.Portrait:
constraint.constant = ((UIScreen.MainScreen.Bounds.Width * editTextWidth)/100);
break;
case UIDeviceOrientation.LandscapeLeft:
case UIDeviceOrientation.LandscapeRight:
constraint.constant = ((UIScreen.MainScreen.Bounds.Height * editTextWidth)/100);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.