Это мой первый опыт использования якорей с моими ограничениями на 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 labellist = new List();
List fieldlist = new List();
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) создаются и добавляются в элемент прокрутки, но ничего не отображается. Обычно я использую плагин Cirious FluentLayouts NUGet для создания ограничений, но мне хотелось расширить свои знания об ограничениях, и я пробую привязки. Судя по всему, дела идут не очень хорошо. Может кто-нибудь сказать мне, что я делаю не так?
Это мой первый опыт использования якорей с моими ограничениями на ViewController, и у него, несомненно, есть проблемы. Прямо сейчас я могу скомпилировать и запустить проект без ошибок, и контроллер представления появляется со всем, кроме представления прокрутки, которое (должно) содержать нужные мне подпредставления. Я реализовал систему элементы данных, определенные пользователем, и разработал экран для Android для сбора данных, и теперь мне нужно сделать то же самое для версии приложения для iOS. На данный момент это мой код: [code]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 labellist = new List(); List fieldlist = new List(); 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;
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; } [/code] Как я уже сказал, приложение компилируется и запускается без ошибок. Проблема в том, что ScrollView не отображается. Элементы данных (UILabel и UITextField) создаются и добавляются в элемент прокрутки, но ничего не отображается. Обычно я использую плагин Cirious FluentLayouts NUGet для создания ограничений, но мне хотелось расширить свои знания об ограничениях, и я пробую привязки. Судя по всему, дела идут не очень хорошо. Может кто-нибудь сказать мне, что я делаю не так?