ios - Auto Layout is preventing me from changing a view's center -


one feature of app automatic cropping of image.

the basic idea take picture of piece of paper (think: receipt), , image cropped automatically, after borders of paper determined.

i'm able determine paper's border using opencv. so, next thing change "center" property of each of guides (just 2 horizontal , 2 vertical "lines" can dragged around manually).

then, sometime shortly after make calls change each of 4 guides, else comes along , sets "center" again. (i've overridden "setcenter" prove this). center seems reset this: [uiview(geometry) _applyisenginelayoutvalues].

i can't figure out why happening, or how stop it, has constraints. view simple uibutton. when user taps & drags on finger, action routine gets called changes center. works.

but in case, i'm bringing uiimagepickercontroller. after choose picture, determine paper-bounds, change "guides" centers, , later on "_applyisenginelayoutvalues" sets them back.

any idea what's going on in case? or how can set center of view, , have stay?

the first rule of autolayout can't update frame, bounds or center of view directly.

you must update constraints related view constraints update view.

for instance, first vertical line have horizontal constraints like...

1. leading edge superview = value. 2. width = value. 

this enough (horizontally) place line on screen.

now, if want move line right can't change center must this...

1. create property in view controller this...  @property (nonatomic, weak) iboutlet nslayoutconstraint *verticalline1leadingconstraint; // or if you're coding constraint... @property (nonatomic, strong) nslayoutconstraint *verticalline1leadingconstraint;  2. save constraint in property...  // either use ib ctrl drag constraint property other outlet. // or like...  self.verticalline1leadingconstraint = [nslayotuconstraint ... // code adding constraint...  [self.view addconstraint:self.verticalline1leadingconstraint]; 

now have property pointing constraint.

now, when need "update center" of vertical line 1...

// calculate distance want line edge of superview , set on constraint...  float distancefromedgeofsuperview = // calculated value...  self.verticalline1leadingconstraint.constant = distancefromedgeofsuperview;  [self.view layoutifneeded]; 

this update position of view , won't errors.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -