Xamarin.iOS 代码中添加约束练习

Xamarin.iOS 代码中添加约束练习

在代码中通过约束,创建如下界面:

  1. CheckBox使用第三方库:BEMCheckBox
  2. 使用Visual Format Language(VFL)视图约束,规则参见iOS开发文档:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html
  3. 对子View进行如下设置:TranslatesAutoresizingMaskIntoConstraints = false;

 

  1 using System;
  2 using System.Drawing;
  3 
  4 using CoreGraphics;
  5 using Foundation;
  6 using MyPassword.Utils;
  7 using SaturdayMP.XPlugins.iOS;
  8 using UIKit;
  9 
 10 namespace MyPassword.iOS.View.CheckContainer
 11 {
 12     public partial class CheckBoxContainerUIView : UIView
 13     {
 14         public CheckBoxContainerUIView()
 15         {
 16             Initialize();
 17         }
 18 
 19         public CheckBoxContainerUIView(IntPtr handle) : base(handle)
 20         {
 21             Initialize();
 22         }
 23 
 24 
 25         public CheckBoxContainerUIView(RectangleF bounds) : base(bounds)
 26         {
 27             Initialize();
 28         }
 29 
 30         void Initialize()
 31         {
 32             var checkboxNum = CreateCheckBox();
 33             checkboxNum.On = true;
 34             var checkBoxUpper = CreateCheckBox();
 35             checkBoxUpper.On = true;
 36             var checkboxSign = CreateCheckBox();
 37             checkboxSign.On = true;
 38             var checkBoxLower = CreateCheckBox();
 39             checkBoxLower.On = true;
 40             
 41 
 42             AddSubview(checkboxNum);
 43             AddSubview(checkBoxUpper);
 44             AddSubview(checkboxSign);
 45             AddSubview(checkBoxLower);
 46 
 47 
 48             var labelNum = CreateLabel("Numbers");
 49             var labelUpper = CreateLabel("Upper Case");
 50             var labelSign = CreateLabel("Signs");
 51             var labelLower = CreateLabel("Lower Case");
 52             
 53 
 54             AddSubview(labelNum);
 55             AddSubview(labelUpper);
 56             AddSubview(labelSign);
 57             AddSubview(labelLower);
 58 
 59 
 60             var viewsDictionary = NSDictionary.FromObjectsAndKeys(
 61                 new NSObject[] 
 62                 {
 63                     checkboxNum,
 64                     checkBoxUpper,
 65                     checkboxSign,
 66                     checkBoxLower,
 67                     labelNum,
 68                     labelUpper,
 69                     labelSign,
 70                     labelLower
 71                 }, 
 72                 new NSObject[] 
 73                 {
 74                     new NSString("checkboxNum"),
 75                     new NSString("checkBoxUpper") ,
 76                     new NSString("checkboxSign"),
 77                     new NSString("checkBoxLower"),
 78                     new NSString("labelNum") ,
 79                     new NSString("labelUpper"),
 80                     new NSString("labelSign"),
 81                     new NSString("labelLower")
 82                 });
 83             var metrics = new NSDictionary();
 84 
 85 
 86             int labelWidth = (int)Bounds.Width / 2 - 18 - 2 - 8 - 2;
 87 
 88             AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-2-[checkboxNum(18)]-(==8)-[labelNum("+ labelWidth + ")]-2-[checkboxSign(18)]-(==8)-[labelSign(" + labelWidth + ")]-2-|", 0, new NSDictionary(), viewsDictionary));
 89             AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-8-[checkboxNum(18)]-(==8)-[checkBoxUpper(==checkboxNum)]-|", 0, new NSDictionary(), viewsDictionary));
 90             AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-8-[checkboxSign(18)]-(==8)-[checkBoxLower(==checkboxSign)]-|", 0, new NSDictionary(), viewsDictionary));
 91             AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-2-[checkBoxUpper(18)]-(==8)-[labelUpper(" + labelWidth + ")]-2-[checkBoxLower(18)]-(==8)-[labelLower(" + labelWidth + ")]-2-|", 0, new NSDictionary(), viewsDictionary));
 92             AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-8-[labelNum(18)]-(==8)-[labelUpper]-8-|", 0, new NSDictionary(), viewsDictionary));
 93             AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-8-[labelSign(18)]-(==8)-[labelLower]-8-|", 0, new NSDictionary(), viewsDictionary));
 94 
 95         }
 96 
 97         private BEMCheckBox CreateCheckBox()
 98         {
 99             var checkBox = new BEMCheckBox(new CoreGraphics.CGRect(140, 60, 18, 18))
100             {
101                 BoxType = BEMBoxType.Circle,
102                 OnAnimationType = BEMAnimationType.Stroke,
103                 OffAnimationType = BEMAnimationType.Stroke,
104                 OnTintColor = UIColor.Blue,
105                 OnCheckColor = UIColor.Blue
106 
107             };
108             checkBox.TranslatesAutoresizingMaskIntoConstraints = false;
109             return checkBox;
110         }
111 
112         private UILabel CreateLabel(string content)
113         {
114             var label = new UILabel
115             {
116                 Text = content
117             };
118             int width = (int)Bounds.Width / 2 - 18 - 2 - 8;
119             label.Frame = new RectangleF(2, 200, width, 18);
120             label.TranslatesAutoresizingMaskIntoConstraints = false;
121             return label;
122         }
123 
124         private void Checkbox_AnimationDidStopForCheckBox(object sender, EventArgs e)
125         {
126             var checkbox = sender as BEMCheckBox;
127             DebugUtil.WriteLine("Checkbox_AnimationDidStopForCheckBox");
128             if (checkbox == null)
129                 return;
130             DebugUtil.WriteLine(""+checkbox.On);
131         }
132     }
133 }
View Code

 

posted @ 2017-12-19 19:01  Devin.Zhou  阅读(360)  评论(0编辑  收藏  举报