ios - Replace custom UIViews arranged by Interface Builder with UILabels programmatically -
using interface builder
, have built long scrollview
filled custom uiviews
, regular uiviews
, stackviews
, uilabels
, uibuttons
, etc.
for of custom uiviews, if not have data, want replace them uilabel says "no data available
" , want able set margins , center text of uilabel.
what's best/easiest way programmatically in viewcontroller
given views arranged using interface builder?
thanks in advance!
you can adding uilabel, simple constraints, on views want cover instead of inside them if want ensure aren't messing controls don't, well, control.
i set simple test app show how method can work
this has stack view images in it, text view, , button trigger sample.
you should able apply method views determine in code have no data show, , want show placeholder, in example i've set iboutletcollection has both stack view , text view in it, , running on both views when button pressed.
all need provide placeholder text , view want replace method
/// method hide view , put placeholder label in view's superview, centered in target view's frame. - (void)showplaceholdertext:(nsstring *)placeholder forview:(uiview *)view { // build placeholder same frame target view uilabel *placeholderlabel = [[uilabel alloc] initwithframe:view.frame]; placeholderlabel.textalignment = nstextalignmentcenter; placeholderlabel.text = placeholder; placeholderlabel.translatesautoresizingmaskintoconstraints = no; // hide target view view.hidden = yes; // put our placeholder superview, overtop target view [view.superview addsubview:placeholderlabel]; // set constraints ensure placeholder label stays positioned correctly [view.superview addconstraint:[nslayoutconstraint constraintwithitem:view attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:placeholderlabel attribute:nslayoutattributetop multiplier:1.0f constant:0.0f]]; [view.superview addconstraint:[nslayoutconstraint constraintwithitem:view attribute:nslayoutattributeright relatedby:nslayoutrelationequal toitem:placeholderlabel attribute:nslayoutattributeright multiplier:1.0f constant:0.0f]]; [view.superview addconstraint:[nslayoutconstraint constraintwithitem:view attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:placeholderlabel attribute:nslayoutattributebottom multiplier:1.0f constant:0.0f]]; [view.superview addconstraint:[nslayoutconstraint constraintwithitem:view attribute:nslayoutattributeleft relatedby:nslayoutrelationequal toitem:placeholderlabel attribute:nslayoutattributeleft multiplier:1.0f constant:0.0f]]; }
the constraints added placeholder should keep positioned correctly, through rotation or other layout activity in view.
Comments
Post a Comment