Relative positioning of drawn elements with autolayout in iOS using Xamarin -


i working on xamarin ios application using autolayout position elements inside views. want draw elements , position them depending of other subviews positioned via autolayout.

my problem is, draw method called before layout of subviews created , before positioned in view, can't pass relative position starting point view drawn. able adding drawn subviews in viewdidlayoutsubviews not in viewdidload method. think not elegant solution , wondering if there other way position elements have drawn passing relative starting point?

here example code:

a viewcontroller display elements:

public class questionviewcontroller : basecontroller {          private uilabel _infolabel;         private uibutton _button1;          public override void viewdidload() {              uiview containerview = view;             containerview.backgroundcolor = uicolor.white;              var constraints = new list<nslayoutconstraint>();              _infolabel = new uilabel()             {                 isaccessibilityelement = true,                 translatesautoresizingmaskintoconstraints = false,                 text = "my info label",                 linebreakmode = uilinebreakmode.wordwrap,                 lines = 2,                 font = uicore.textfont,                 textcolor = uicore.textcolor             };             containerview.addsubview(_infolabel);              constraints.add(nslayoutconstraint.create(_infolabel, nslayoutattribute.top, nslayoutrelation.equal, containerview, nslayoutattribute.bottom, 1f, 10f));             constraints.add(nslayoutconstraint.create(_infolabel, nslayoutattribute.leading, nslayoutrelation.equal, containerview, nslayoutattribute.leading, 1f, 10f));             constraints.add(nslayoutconstraint.create(_infolabel, nslayoutattribute.trailing, nslayoutrelation.equal, containerview, nslayoutattribute.trailing, 1f, -10f));              containerview.addconstraints(constraints.toarray());              base.viewdidload();             }          public override void viewdidlayoutsubviews() {              var containerview = view;              var mycircle = new circleview(new rectanglef((float)_infolabel.frame.x, (float)_infolabel.frame.bottom, 60, 60), uicolor.red, "test");             containerview.addsubview(mycircle);              base.viewdidlayoutsubviews();         }     } 

the circleview draw circle:

public class circleview : uiview {         private uicolor _color;         private string _label;          public circleview()         {             initialize();         }          public circleview(rectanglef bounds, uicolor color, string label) : base(bounds) {             _label = label;             _color = color;              initialize();         }          void initialize()         {             backgroundcolor = uicolor.white;         }          public override void draw(cgrect rect) {              var context = uigraphics.getcurrentcontext();             context.setfillcolor(_color.cgcolor);             context.fillellipseinrect(rect);            }     } 

i found answer working me.

in viewdidload method create subview, add circleview subview , add subview containerview. circleview gets starting point 0,0 , add constraints subview position inside containerview:

var dummyview = new uiview(); dummyview.addsubview(new circleview(new rectanglef(0f, 0f, 60, 60), uicolor.red, "test");)  containerview.addsubview(dummyview); constraints.add(nslayoutconstraint.create(dummyview, nslayoutattribute.top, nslayoutrelation.equal, _infolabel, nslayoutattribute.bottom, 1f, 10f)); constraints.add(nslayoutconstraint.create(dummyview, nslayoutattribute.leading, nslayoutrelation.equal, containerview, nslayoutattribute.leading, 1f, 10f)); 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -