Reputation: 315
I want to increase the height of mainview dynamically as the content size of scrollview increases. As you see in my image, there is white space left in scrollview and I want that to be covered by my main view. I don't know initially what will be the content size of scrollview as it is increasing at runtime.
Here is my following code:
int x= 17;
int y = 27;
int width = 287;
int height = 181;
int mainview_height = 568;
int x_spacer = 0;
int y_spacer = 0;
int width_spacer = 320;
int height_spacer = 27;
UIView *view_spacer = [[UIView alloc]init];
[view_spacer setBackgroundColor:[UIColor greenColor]];
view_spacer.translatesAutoresizingMaskIntoConstraints = false;
[self.mainView addSubview:view_spacer];
UIView *view1 = [[UIView alloc]init];
[view1 setBackgroundColor:[UIColor whiteColor]];
view1.translatesAutoresizingMaskIntoConstraints = false;
[self.mainView addSubview:view1];
[view_spacer addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeHeight multiplier:width_spacer/height_spacer constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeTop multiplier:1 constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width_spacer/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view.spacer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeBottom multiplier:1 constant:0.0f]];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier:width/height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
My view1 and view_spacer are not getting displayed. What is wrong in my code?
Upvotes: 0
Views: 231
Reputation: 3358
Here is your solution. I have tried this code in a test project. Its working fine just needs a slight change. Paste below code instead of your code and check it. I have changed the second view color to red to identify better.
float x= 17;
float y = 27;
float width = 287;
float height = 181;
float mainview_height = 568;
float x_spacer = 0;
float y_spacer = 0;
float width_spacer = 320;
float height_spacer = 27;
UIView *view_spacer = [[UIView alloc]init];
[view_spacer setBackgroundColor:[UIColor greenColor]];
view_spacer.translatesAutoresizingMaskIntoConstraints = false;
[self.mainView addSubview:view_spacer];
UIView *view1 = [[UIView alloc]init];
[view1 setBackgroundColor:[UIColor redColor]];
view1.translatesAutoresizingMaskIntoConstraints = false;
[self.mainView addSubview:view1];
[view_spacer addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeHeight multiplier:width_spacer/height_spacer constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeTop multiplier:1 constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width_spacer/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeBottom multiplier:1 constant:0.0f]];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier:width/height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[self.mainView layoutIfNeeded];
Upvotes: 2