将 translateAutoResizingMaskIntoConstraints 设置为 NO 时不显示 UIView
UIView not displayed with translateAutoResizingMaskIntoConstraints set to NO
我想随着滚动视图内容大小的增加动态增加主视图的高度。正如您在我的图像中看到的那样,滚动视图中留下了白色 space,我希望它被我的主视图覆盖。我最初不知道滚动视图的内容大小是多少,因为它在运行时会增加。
这是我的以下代码:
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]];
我的 view1 和 view_spacer 没有显示。我的代码有什么问题?
这是您的解决方案。我在测试项目中尝试过这段代码。它的工作正常只需要稍微改变一下。粘贴下面的代码而不是您的代码并检查它。我已将第二个视图颜色更改为红色以更好地识别。
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];
我想随着滚动视图内容大小的增加动态增加主视图的高度。正如您在我的图像中看到的那样,滚动视图中留下了白色 space,我希望它被我的主视图覆盖。我最初不知道滚动视图的内容大小是多少,因为它在运行时会增加。
这是我的以下代码:
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]];
我的 view1 和 view_spacer 没有显示。我的代码有什么问题?
这是您的解决方案。我在测试项目中尝试过这段代码。它的工作正常只需要稍微改变一下。粘贴下面的代码而不是您的代码并检查它。我已将第二个视图颜色更改为红色以更好地识别。
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];