ios - Setting constraints programmatically -
i'm experimenting how use uiscrollview. after trouble, got hang of it. i've seem hit snag.
in simple app, have scroll view , in order work, have set view's bottom space scrollview constraint 0 described here , works fine. i'm doing through ib.
now i've come across scenario have part programmatically. i've added below code in viewdidload
method.
nslayoutconstraint *bottomspaceconstraint = [nslayoutconstraint constraintwithitem:self.view attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:self.scrollview attribute:nslayoutattributebottom multiplier:1.0 constant:0.0]; [self.view addconstraint:bottomspaceconstraint];
but doesn't seem work. outputs following message in console window adn don't know make of it.
unable simultaneously satisfy constraints. @ least 1 of constraints in following list 1 don't want. try this: (1) @ each constraint , try figure out don't expect; (2) find code added unwanted constraint or constraints , fix it. (note: if you're seeing nsautoresizingmasklayoutconstraints don't understand, refer documentation uiview property translatesautoresizingmaskintoconstraints) ( "", "" )
can please tell me how this? i've attached demo project here can better idea on issue.
update:
first off responses. using ways mentioned in answers able working. ina different scenario not. i'm trying load view onto viewcontroller programatically.
if may explain further. there 2 view controllers. first 1 being uitableviewcontroller
, second 1 uiviewcontroller
. inside uiscrollview
. there multiple uiview
s , of views' height exceeds normal height of screen.
the uitableviewcontroller
displays list of options. based on user's selection, particular uiview
out of lot loaded uiviewcontroller
uiscrollview
.
in scenario, above method doesn't work. scrolling isn't happening. have different since i'm loading view separately?
i've uploaded demo project here can see in action. please see email.xib
, select email table view list.
based upon review of code, few comments:
you don't need adjust constraints when view appears. if find doing this, means haven't configured storyboard correctly (or @ least, not efficiently). time need set/create constraints either (a) you're adding views programmatically (which i'm suggesting more work it's worth); or (b) need runtime adjustment of constraints (see third bullet under point 3, below).
i don't mean belabor it, project had bunch of redundant code. e.g.
you setting frame scroll view, governed constraints, nothing (i.e. when constraints applied, manually set
frame
settings replaced). in general, in auto layout, don't try changingframe
directly: edit constraints. but, no changing of constraints needed @ anyway, point moot.you setting content size scroll view, in auto layout, that, too, governed constraints (of subviews), unnecessary.
you setting constraints scrollview (which zero), weren't adding view nib scrollview, defeating intent there, too. original question how change bottom constraint of scroll view. bottom constraint zero, see no reason set 0 again.
i'd suggest more radical simplification of project:
you're making life harder on storing views in nibs. it's easier if stay within the storyboard world. can nib stuff if need to, why make life hard on yourself?
use cell prototypes facilitate design of cells in table. can define segues go cells next scene. eliminates need write
didselectrowatindexpath
orprepareforsegue
code. clearly, if have need pass next scene, means useprepareforsegue
, nothing you've presented far requires that, i've commented out in examples.assuming looking practical example of programmatically changing constraints, i've set scene text view change height programmatically, based upon text in text view. always, rather iterating through constraints find 1 in question, when altering existing constraint ib created me, think it's far more efficient set
iboutlet
constraint, , editconstant
property constraint directly, that's i've done. set view controller delegate of text view, , wrotetextviewdidchange
updated text view's height constraint:#pragma mark - uitextviewdelegate - (void)textviewdidchange:(uitextview *)textview { self.textviewheightconstraint.constant = textview.contentsize.height; [self.scrollview layoutifneeded]; }
note, text view has 2 height constraints, mandatory minimum height constraint, , medium priority constraint change above based upon amount of text. main point illustrates practical example of changing constraints programmatically. shouldn't have muck around scrollview's bottom constraint @ all, shows real-world example of when might want adjust constraint.
when add scrollview in ib, automatically constraints need. don't want adding constraint programmatically (at least not without removing existing bottom constraint).
two approaches might simpler:
create
iboutlet
existing bottom constraint,scrollviewbottomconstraint
. can doself.scrollviewbottomconstraint.constant = 0.0;
or create view in ib bottom constraint 0.0 , don't have programmatically @ all. if want layout long scrollview , it's subviews, select controller, set it's simulated metrics "inferred" "free form". can change size of view, set scrollview's top , bottom constraints zero, layout want inside scroll view, , when view presented @ runtime, view resized appropriately, , because you've defined scrollview's top , bottom constraints 0.0, resized properly. looks bit odd in ib, works charm when app runs.
if you're determined add new constraint, either programmatically remove old bottom constraint, or set old bottom constraints' priority down low possible, , way new constraint (with higher priority) take precedence, , old, low-priority bottom constraint gracefully not applied.
but don't want add new constraint.
Comments
Post a Comment