Reputation: 2224
I have tried a lot to set auotolayout constraint for UITextView. But when i run , i get blank screen . I tried the same way in UIButton , that worked !!!!!
Is there any property to be set to assign constraint for UITextView ????
UITextView *text1;
text1=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];
[text1 setBackgroundColor:[UIColor blueColor]];
set.text1=@"asjdgajsdbasbfbsdfbsdbfbsd";
[text1 sizeToFit];
set.translatesAutoresizingMaskIntoConstraints=NO;
[self.view addSubview:text1];
NSLayoutConstraint *constraint;
constraint = [NSLayoutConstraint constraintWithItem:text1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:1.f];
[self.view addConstraint:constraint];
Upvotes: 1
Views: 1343
Reputation: 3700
Yes like say @P.Sami you need more constraints, because line
set.translatesAutoresizingMaskIntoConstraints=NO;
disable autoresizeMask it mean your frame not working more. And all frames colculated by constraints.
in your event only one constraint describe this: constraint = [NSLayoutConstraint constraintWithItem:text1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:1.f];
This constraint told: "text1.bottom like self.view.bottom * 1 + 1" First it's not equal bottom edge text1 and superview but offset on 1 point down. Second we don't know what about with heights, wight, x, y?
For simple example just add addition constrints:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[text]|" options:0 metrics:nil views:@{text1}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[text]|" options:0 metrics:nil views:@{text1}]];
it's told to text1 be like self.view frame by Verticale and Horizontale
Upvotes: 3