这是我第一次尝试在 ViewController 上使用锚点,但毫无疑问它存在问题。现在我能够编译并运行该项目,不会出现错误,并且视图控制器会显示除滚动视图之外的所有内容,滚动视图(应该)包含我需要的子视图。
我已经实现了一个用户定义数据元素的系统,并在 Android 中开发了一个屏幕来捕获数据,现在我需要对 iOS 版本的应用程序执行相同的操作。到目前为止,这是我的代码:
public string action;
public string emplyoyeeid;
public nfloat imageHeight = 0;
public nfloat imageWidth = 0;
public byte[] imageBytes;
DataSet dsAdditionalData = new DataSet();
DataInterfaceWeb.DataInterface myService = new DataInterfaceWeb.DataInterface();
public AdditionalData (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
var NavBarHeight = UIApplication.SharedApplication.StatusBarFrame.Size.Height +
(NavigationController?.NavigationBar.Frame.Height ?? 0.0);
nfloat height = View.Bounds.Height;
nfloat width = View.Bounds.Width;
float padding = 10.0f;
int n = 20;
float h = 50.0f;
float w = 50.0f;
UIImageView banner = new UIImageView();
UIImageView employeeimage = new UIImageView();
employeeimage.Frame = new RectangleF(0f, 0f, (float)imageWidth, (float)imageHeight);
UIImage Mybitmap = Utils.GetImagefromByteArray(imageBytes);
employeeimage.Image = Mybitmap;
UIButton btnSave = new UIButton();
btnSave.SetTitle("Save", UIControlState.Normal);
btnSave.SetTitleColor(UIColor.Black, UIControlState.Normal);
btnSave.BackgroundColor = UIKit.UIColor.FromRGB(255, 215, 0);
btnSave.HorizontalAlignment = UIControlContentHorizontalAlignment.Left;
btnSave.Layer.BorderWidth = 1;
btnSave.Frame = new CGRect(0, 0f, 75f, 26);
UILabel lblAgeAsOf = new UILabel();
lblAgeAsOf.Text = "Age as of: ";
List<UILabel> labellist = new List<UILabel>();
List<UITextField> fieldlist = new List<UITextField>();
UIView line = new UIView();
line.BackgroundColor = UIColor.Clear;
line.Frame = new CGRect(0, 0, View.Frame.Width, 2);
var maskLayer = new CAShapeLayer();
UIBezierPath bezierPath = UIBezierPath.FromRoundedRect(line.Bounds, (UIRectCorner.TopLeft | UIRectCorner.BottomLeft), new CGSize(18.0, 18.0));
maskLayer.Path = bezierPath.CGPath;
maskLayer.Frame = line.Bounds;
maskLayer.StrokeColor = UIColor.Black.CGColor; //set the borderColor
maskLayer.FillColor = UIColor.Black.CGColor; //set the background color
maskLayer.LineWidth = 1; //set the border width
line.Layer.AddSublayer(maskLayer);
View.BackgroundColor = UIKit.UIColor.FromRGB(255, 215, 0);
UIScrollView scrollView = new UIScrollView();
scrollView = new UIScrollView
{
Frame = new CGRect(0, 0, View.Frame.Width, h + 2 * padding),
ContentSize = new SizeF((w + padding) * 7, (h + padding) * n),
BackgroundColor = UIColor.White,
AutoresizingMask = UIViewAutoresizing.FlexibleWidth
};
banner.Image = UIImage.FromResource(null, "OML_iOS.Resources.drawable.Banner.jpg");
banner.Frame = new RectangleF(0, 0, (float)width, 73);
banner.ContentMode = UIViewContentMode.ScaleAspectFit;
dsAdditionalData = myOMLService.GetAdditionalDataFields(Utils.MyUser.Username, Utils.MyUser.Password, Utils.MyCompanyInfo.Company);
if (dsAdditionalData.Tables[0].Rows.Count > 0)
{
{
foreach (DataRow datarow in dsAdditionalData.Tables[0].Rows)
{
UILabel lbl = new UILabel();
lbl.AccessibilityLabel = datarow["ID"].ToString(); // to retreive any data
lbl.Text = datarow["Title"].ToString();
lbl.TextColor = UIColor.Blue;
labellist.Add(lbl);
UITextField txt = new UITextField();
txt.AccessibilityLabel = datarow["ID"].ToString();
if (action.ToUpper() == "EDITREG" || action.ToUpper() == "EDIT")
{
txt.Text = myOMLService.GetAdditionalDataFieldDataByID(Utils.MyUser.Username, Utils.MyUser.Password, emplyoyeeid, datarow["ID"].ToString());
}
fieldlist.Add(txt);
}
}
}
scrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
int x = 0;
UITextField MyTextField = new UITextField();
foreach (UILabel Mylabel in labellist) {
scrollView .Add(Mylabel);
MyTextField = fieldlist[x];
scrollView.Add(MyTextField);
if (x == 0)
{
Mylabel.TopAnchor.ConstraintEqualTo(scrollView.TopAnchor, 5f).Active = true;
Mylabel.LeadingAnchor.ConstraintEqualTo(scrollView.LeadingAnchor).Active = true;
MyTextField.TopAnchor.ConstraintEqualTo(scrollView.TopAnchor, 5f).Active = true;
MyTextField.LeadingAnchor.ConstraintEqualTo(scrollView.TrailingAnchor).Active = true;
} else
{
Mylabel.TopAnchor.ConstraintEqualTo(labellist[x].BottomAnchor, 5f).Active = true;
Mylabel.LeadingAnchor.ConstraintEqualTo(scrollView.LeadingAnchor).Active = true;
MyTextField.TopAnchor.ConstraintEqualTo(labellist[x].TopAnchor, 5f).Active = true;
MyTextField.LeadingAnchor.ConstraintEqualTo(labellist[x].TrailingAnchor).Active = true;
}
x += 1;
}
var margins = View.LayoutMarginsGuide;
View.Add(banner);
View.Add(employeeimage);
View.Add(btnSave);
View.Add(lblAgeAsOf);
View.Add(line);
View.Add(scrollView);
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
banner.LeadingAnchor.ConstraintEqualTo(margins.LeadingAnchor).Active = true;
banner.TrailingAnchor.ConstraintEqualTo(margins.TrailingAnchor).Active = true;
banner.TopAnchor.ConstraintEqualTo(TopLayoutGuide.GetBottomAnchor()).Active = true;
employeeimage.LeadingAnchor .ConstraintEqualTo(margins.LeadingAnchor).Active = true;
employeeimage.TopAnchor.ConstraintEqualTo(banner.BottomAnchor).Active = true;
btnSave.LeadingAnchor.ConstraintEqualTo(employeeimage.TrailingAnchor, 10f).Active = true;
btnSave.TopAnchor.ConstraintEqualTo(employeeimage.TopAnchor).Active = true;
lblAgeAsOf.LeadingAnchor.ConstraintEqualTo(margins.LeadingAnchor).Active = true;
lblAgeAsOf.TopAnchor.ConstraintEqualTo(employeeimage.BottomAnchor).Active = true;
line.LeadingAnchor.ConstraintEqualTo(margins.LeadingAnchor).Active = true;
line.TopAnchor.ConstraintEqualTo(lblAgeAsOf.BottomAnchor,5f).Active = true;
scrollView.LeadingAnchor.ConstraintEqualTo(margins.LeadingAnchor).Active = true;
scrollView.TopAnchor.ConstraintEqualTo(line.BottomAnchor).Active = true;
scrollView.BottomAnchor.ConstraintEqualTo(margins.BottomAnchor).Active = true;
}
正如我所说,该应用程序编译并运行时没有错误。问题是
scrollView
没有出现。数据元素(UILabel 和 UITextField)已创建并添加到 scrollView
,但什么也没有出现。我通常使用 Cirrious FluentLayouts NUGet 插件来创建约束,但我想扩展我对约束的了解,并且我正在尝试anchor
。显然进展不顺利。谁能告诉我我做错了什么?
好吧,我无法弄清楚为什么滚动视图没有出现,并且没有人能够提供帮助(显然)。所以我回到了我在其他视图上使用的 Cirrious FluentLayouts NuGet 包。这(显然)解决了滚动视图显示的问题。我还能够在此链接使用 xamarin.ios c# 以编程方式添加嵌套堆栈视图找到显示用户定义字段的解决方案。我的代码:
public override void ViewDidLoad()
{
var NavBarHeight = UIApplication.SharedApplication.StatusBarFrame.Size.Height +
(NavigationController?.NavigationBar.Frame.Height ?? 0.0);
nfloat height = View.Bounds.Height;
nfloat width = View.Bounds.Width;
float padding = 10.0f;
int n = 20;
float h = 50.0f;
float w = 50.0f;
UIStackView verticalStack = new UIStackView();
verticalStack.Axis = UILayoutConstraintAxis.Vertical;
verticalStack.TranslatesAutoresizingMaskIntoConstraints = false;
UIImageView banner = new UIImageView();
UIImageView employeeimage = new UIImageView();
employeeimage.Frame = new RectangleF(0f, 0f, (float)imageWidth, (float)imageHeight);
UIImage Mybitmap = Utils.GetImagefromByteArray(imageBytes);
employeeimage.Image = Mybitmap;
UIButton btnSave = new UIButton();
btnSave.SetTitle("Save", UIControlState.Normal);
btnSave.SetTitleColor(UIColor.Black, UIControlState.Normal);
btnSave.BackgroundColor = UIKit.UIColor.FromRGB(255, 215, 0);
btnSave.HorizontalAlignment = UIControlContentHorizontalAlignment.Left;
btnSave.Layer.BorderWidth = 1;
btnSave.Frame = new CGRect(0, 0f, 75f, 26);
UILabel lblAgeAsOf = new UILabel();
lblAgeAsOf.Text = "Age as of: ";
List<UILabel> labellist = new List<UILabel>();
List<UITextField> fieldlist = new List<UITextField>();
UIView line = new UIView();
line.BackgroundColor = UIColor.Clear;
line.Frame = new CGRect(0, 0, View.Frame.Width, 2);
var maskLayer = new CAShapeLayer();
UIBezierPath bezierPath = UIBezierPath.FromRoundedRect(line.Bounds, (UIRectCorner.TopLeft | UIRectCorner.BottomLeft), new CGSize(18.0, 18.0));
maskLayer.Path = bezierPath.CGPath;
maskLayer.Frame = line.Bounds;
maskLayer.StrokeColor = UIColor.Black.CGColor; //set the borderColor
maskLayer.FillColor = UIColor.Black.CGColor; //set the background color
maskLayer.LineWidth = 1; //set the border width
line.Layer.AddSublayer(maskLayer);
UILabel lblName = new UILabel();
lblName.Text = name;
View.BackgroundColor = UIKit.UIColor.FromRGB(255, 215, 0);
UIScrollView scrollView = new UIScrollView();
scrollView = new UIScrollView
{
Frame = new CGRect(0, 0, View.Frame.Width, h + 2 * padding),
ContentSize = new SizeF((w + padding) * 7, (h + padding) * n),
BackgroundColor = UIColor.White,
AutoresizingMask = UIViewAutoresizing.FlexibleWidth
};
banner.Image = UIImage.FromResource(null, "iOS.Resources.drawable.Banner.jpg");
banner.Frame = new RectangleF(0, 0, (float)width, 73);
banner.ContentMode = UIViewContentMode.ScaleAspectFit;
dsAdditionalData = myService.GetAdditionalDataFields(Utils.MyUser.Username, Utils.MyUser.Password, Utils.MyCompanyInfo.Company);
if (dsAdditionalData.Tables[0].Rows.Count > 0)
{
{
foreach (DataRow datarow in dsAdditionalData.Tables[0].Rows)
{
UIStackView horizontalStack = new UIStackView();
horizontalStack.Distribution = UIStackViewDistribution.EqualSpacing;
horizontalStack.Axis = UILayoutConstraintAxis.Horizontal;
// UIStackView should use AddArrangedSubview() to add subviews.
UILabel lbl = new UILabel();
lbl.AccessibilityLabel = datarow["ID"].ToString();
lbl.Text = datarow["Title"].ToString();
lbl.TextColor = UIColor.Blue;
//labellist.Add(lbl);
UITextField txt = new UITextField();
txt.Placeholder = "Enter " + datarow["Title"].ToString();
txt.AccessibilityLabel = datarow["ID"].ToString(); // to link the data input to the field
if (action.ToUpper() == "EDITREG" || action.ToUpper() == "EDIT")
{
txt.Text = myService.GetAdditionalDataFieldDataByID(Utils.MyUser.Username, Utils.MyUser.Password, employeeid, datarow["ID"].ToString());
}
horizontalStack.AddArrangedSubview(lbl);
horizontalStack.AddArrangedSubview(txt);
//fieldlist.Add(txt);
verticalStack.AddArrangedSubview(horizontalStack);
}
}
}
scrollView.Add(verticalStack);
scrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
UITextField MyTextField = new UITextField();
View.Add(banner);
View.Add(employeeimage);
View.Add(btnSave);
View.Add(lblName);
View.Add(lblAgeAsOf);
View.Add(line);
View.Add(scrollView);
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints
(
banner.AtTopOf(View, (float)NavBarHeight),
banner.AtRightOf(View, 0),
banner.AtLeftOf(View, 0),
playerimage.Below(banner, 2),
playerimage.Height().EqualTo(180),
playerimage.Width().EqualTo(180),
btnSave.AtTopOf(playerimage),
btnSave.ToRightOf(employeeimage, 5),
lblName.Below(employeeimage, 2),
lblName.WithSameWidth(banner),
lblAgeAsOf.Below(lblName, 0),
lblAgeAsOf.WithSameWidth(banner),
line.Below(lblAgeAsOf, 0),
line.WithSameWidth(banner),
line.Height().EqualTo(2),
scrollView.Below(line, 0),
scrollView.AtBottomOf(View, 10),
scrollView.AtLeftOf(View, 10),
scrollView.AtRightOf(View, 10)
);
}