ios - Add subview to UITextView and using autolayout -


i have problem adding subview uitextview using autolayout:

in subtextview : uitextview .m file:

 - (instancetype)initwithframe:(cgrect)frame {     if(self = [super initwithframe:frame]) {         _baseline = [[uiview alloc] init];         [self addsubview:_baseline];         [self setbackgroundcolor: [uicolor blackcolor]];         _baseline.translatesautoresizingmaskintoconstraints = no;         [self setneedsupdateconstraints];     } } - (void)updateconstraints {     [super updateconstraints];     nslayoutconstraint *constraint = nil;     constraint = [nslayoutconstraint constraintwithitem:self.baseline                                           attribute:nslayoutattributetrailing                                           relatedby:nslayoutrelationequal                                              toitem:self                                           attribute:nslayoutattributetrailing                                          multiplier:1.f                                            constant:0.f]; [self addconstraint:constraint];  constraint = [nslayoutconstraint constraintwithitem:self.baseline                                           attribute:nslayoutattributeleading                                           relatedby:nslayoutrelationequal                                              toitem:self                                           attribute:nslayoutattributeleading                                          multiplier:1.f                                            constant:0.f]; [self addconstraint:constraint];  constraint = [nslayoutconstraint constraintwithitem:self.baseline                                           attribute:nslayoutattributecentery                                           relatedby:nslayoutrelationequal                                              toitem:self                                           attribute:nslayoutattributecentery                                          multiplier:1.f                                            constant:0.f]; [self addconstraint:constraint];  } 

but, not work... self.baseline's frame (0, 0, 0, 0)... can help?

edited:

i set break point @ layout subviews, @ time, when layoutsubviews subviews inited, , uitextview :

<inventoryoverviewlistingnametextview: 0x7fd92b2a1800;  baseclass = uitextview;  frame = (234 36; 399 70.2);  text = '11111111';  clipstobounds = yes;  gesturerecognizers = <nsarray: 0x7fd92cbd8ca0>;  layer = <calayer: 0x7fd92cb08900>;  contentoffset: {-0, 10};  contentsize: {399, 70.199997425079346}> 

but frame of baseline is:

<uiview: 0x7fd92cb90710;  frame = (0 -10; 399 0.5); layer = <calayer: 0x7fd92cb907e0>>.  

do not know how make y = -10 y = 69.7.

i feel little scrollview, solve add uiview contentview on uiscrollview, , uiscrollview added on uiview. set contentview's top scrollview's top, contentview's heigh, , !!!(this important or autolayout won't work properly)

contentview's left , right uiview rather scrollview.

but not see works uitextview...

anyone can help?

the frame (0,0,0,0) because default 1 comes whit [[uiview alloc] init].

have tried defining view setting frame?

- (instancetype)initwithframe:(cgrect)frame {     if(self = [super initwithframe:frame]) {         //add frame, not matter size         _baseline = [[uiview alloc] initwithframe:cgrectmake(0,0,20,20)];          //just make sure it's there let give color         [_baseline setbackgroundcolor:[uicolor green]];          [self addsubview:_baseline];          //don't run line yet.         //[self setneedsupdateconstraints];     } } 

if works, found problem, if not, maybe problem on - (void)updateconstraints method.

update:

so, sat down , ran code, got few crashes managed make black box apear inside text view. found box not instantiated when view looking layout subviews, tinkered around , here goes, works, throws few constraints warnings guess can work yourself.

- (instancetype)initwithframe:(cgrect)frame     {         if(self = [super initwithframe:frame]) {         }          return self;     }  -(void)layoutsubviews{         [super layoutsubviews];         if (!_baseline) {             _baseline = [[uiview alloc] init];             [self addsubview:_baseline];             [self setbackgroundcolor: [uicolor blackcolor]];             [self setneedsupdateconstraints];         }      }  - (void)updateconstraints {     [super updateconstraints];     if (_baseline) {         nslayoutconstraint *constraint = nil;             constraint = [nslayoutconstraint constraintwithitem:self.baseline                                                       attribute:nslayoutattributetrailing                                                       relatedby:nslayoutrelationequal                                                          toitem:self                                                       attribute:nslayoutattributetrailing                                                      multiplier:1.f                                                        constant:0.f];             [self addconstraint:constraint];              constraint = [nslayoutconstraint constraintwithitem:self.baseline                                                       attribute:nslayoutattributeleading                                                       relatedby:nslayoutrelationequal                                                          toitem:self                                                       attribute:nslayoutattributeleading                                                      multiplier:1.f                                                        constant:0.f];             [self addconstraint:constraint];              constraint = [nslayoutconstraint constraintwithitem:self.baseline                                                       attribute:nslayoutattributecentery                                                       relatedby:nslayoutrelationequal                                                          toitem:self                                                       attribute:nslayoutattributecentery                                                      multiplier:1.f                                                        constant:0.f];             [self addconstraint:constraint];      } } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -