Superview 不会根据子视图约束增加高度
Superview not increasing in height based on the subviews constraint
我有一个滚动视图和一个单独的 UIView,我在其中放置了一系列带有约束的文本字段和标签,它们完全占据了顶部和底部。我正在尝试根据其子视图约束调整 UIView 的高度,但它不会。发生的事情是视图保持其高度并强制其他文本字段折叠或收缩从而打破约束。
详情
- 每个子视图的优先级值:
压缩 = 750
拥抱 = 250
- UIView 优先级值:
压缩 = 249
拥抱 = 749 设置为低于其余部分。
- 大部分文本框都有纵横比限制。这会导致字段调整。
- 每个子视图彼此之间有 vertical/top/bottom 间距。顶部和底部元素对视图也有顶部和底部约束。
我的代码是什么:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
/* I had to adjust the UIView's width to fill the entire self.view.*/
if(![contentView isDescendantOfView:detailsScrollView]){
CGRect r = contentView.frame;
r.size.width = self.view.frame.size.width;
contentView.frame = r;
[detailsScrollView addSubview:contentView];
}
}
截图
景色
这就是目前发生的情况。在这种情况下,它会强制电子邮件字段缩小。如果我在其上放置一个高度值,它不会缩小,但布局引擎会发现另一个要中断的元素
编辑:
已解决
也许我只是需要休息一下来恢复精神。我之前确实尝试过使用约束,但没有成功。然而,多亏了这个建议,我回去设置约束而不是在这个框架上设置框架,并最终让它工作了。
解决方案:
-(void)viewDidLoad{
[detailsScrollView addSubview:contentView];
[contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[detailsScrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(contentView,detailsScrollView);
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[contentView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil
views:viewsDictionary];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[contentView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDictionary];
NSArray *widthConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[contentView(==detailsScrollView)]-0-|" options:0 metrics:nil views:viewsDictionary];
}
当您使用界面生成器处理 UIScrollView
及其 child UIView
时。通常在 UIScrollView
和它的 child 之间设置顶部、底部、左侧和等宽约束,在您的情况下是 contentView
。
没有这些限制,另一个选项是设置 UIScrollView 的内容大小。这是在引入约束之前使用 UIScrollView 的方式。
So, 1. you should add those constraints programmatically.
通过使用约束,不再需要视图框架来调整视图大小。
So, 2. remove frame setting for your content view.
我对您在 viewDidLayoutMethod 中设置框架的方式不太满意。如果我要在这里这样做,我会从 if statement
.
中取出框架设置
没有if语句的代码如下:
[detailsScrollView addSubview:contentView];
// then set the constraints here after adding the subview.
将此代码放在任何地方,但不要放在您的 viewDidLayoutSubviews 方法中。这将是一个比在 if 语句中设置框架更大的问题。
Note: Originally, if you are going to set frame in the viewDidLayoutSubviews
method. you should do it for all cases. for example for the if case
and the else case. because, next time this method is going to be
called the views will respond to the constraint. and lose its frame.
另一个观察:如果你想让视图响应它的子视图约束,为什么你需要为它设置框架?对吗?
添加约束后,您可能需要调用方法 constraintNeedsUpdate
或其他相关方法。
我有一个滚动视图和一个单独的 UIView,我在其中放置了一系列带有约束的文本字段和标签,它们完全占据了顶部和底部。我正在尝试根据其子视图约束调整 UIView 的高度,但它不会。发生的事情是视图保持其高度并强制其他文本字段折叠或收缩从而打破约束。
详情
- 每个子视图的优先级值: 压缩 = 750 拥抱 = 250
- UIView 优先级值: 压缩 = 249 拥抱 = 749 设置为低于其余部分。
- 大部分文本框都有纵横比限制。这会导致字段调整。
- 每个子视图彼此之间有 vertical/top/bottom 间距。顶部和底部元素对视图也有顶部和底部约束。
我的代码是什么:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
/* I had to adjust the UIView's width to fill the entire self.view.*/
if(![contentView isDescendantOfView:detailsScrollView]){
CGRect r = contentView.frame;
r.size.width = self.view.frame.size.width;
contentView.frame = r;
[detailsScrollView addSubview:contentView];
}
}
截图
景色
这就是目前发生的情况。在这种情况下,它会强制电子邮件字段缩小。如果我在其上放置一个高度值,它不会缩小,但布局引擎会发现另一个要中断的元素
编辑:
已解决
也许我只是需要休息一下来恢复精神。我之前确实尝试过使用约束,但没有成功。然而,多亏了这个建议,我回去设置约束而不是在这个框架上设置框架,并最终让它工作了。
解决方案:
-(void)viewDidLoad{
[detailsScrollView addSubview:contentView];
[contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[detailsScrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(contentView,detailsScrollView);
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[contentView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil
views:viewsDictionary];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[contentView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDictionary];
NSArray *widthConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[contentView(==detailsScrollView)]-0-|" options:0 metrics:nil views:viewsDictionary];
}
当您使用界面生成器处理 UIScrollView
及其 child UIView
时。通常在 UIScrollView
和它的 child 之间设置顶部、底部、左侧和等宽约束,在您的情况下是 contentView
。
没有这些限制,另一个选项是设置 UIScrollView 的内容大小。这是在引入约束之前使用 UIScrollView 的方式。
So, 1. you should add those constraints programmatically.
通过使用约束,不再需要视图框架来调整视图大小。
So, 2. remove frame setting for your content view.
我对您在 viewDidLayoutMethod 中设置框架的方式不太满意。如果我要在这里这样做,我会从 if statement
.
没有if语句的代码如下:
[detailsScrollView addSubview:contentView];
// then set the constraints here after adding the subview.
将此代码放在任何地方,但不要放在您的 viewDidLayoutSubviews 方法中。这将是一个比在 if 语句中设置框架更大的问题。
Note: Originally, if you are going to set frame in the viewDidLayoutSubviews method. you should do it for all cases. for example for the if case and the else case. because, next time this method is going to be called the views will respond to the constraint. and lose its frame.
另一个观察:如果你想让视图响应它的子视图约束,为什么你需要为它设置框架?对吗?
添加约束后,您可能需要调用方法 constraintNeedsUpdate
或其他相关方法。