UIStackView:从xib加载视图并更新subView的高度限制没有反映任何变化吗? [英] UIStackView: Loading views from xib and updating height constraint of subView did not reflecting any changes?
问题描述
我的应用程序中具有以下层次结构
I have the following hierarchy in my application
- UIScrollView
- UIStackView
- UIView 1 // load with xib and added in arrangedSubviews
- UIScrollView 1.1 // horizontal scrolling, fixed height constraint 38
- UIView 1.2 // called it childView. has fixed height 0 (I load the view from xib and add it here dynamically and update its height)
- UIView 1.2.1 // called it New View
- UIView 2
- UIView 3
所以我的问题是当我从xib加载视图并将其添加到UIView1.2
时,也将高度限制0增加到了新添加的子视图的高度,但是什么也不会发生.UIView1.2
height预期不会更新.
So my problem is when I have loaded a view from xib and added it to UIView1.2
also increased height constraint 0 to a height of newly added sub-view but nothing will happen.UIView1.2
height did not update expectedly .
self.constraintChildViewHeight.constant = 95;
[self layoutIfNeeded];
NewView *newView = (NewView *)[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([FieldPhotoView class]) owner:self options:nil]objectAtIndex:0];
[newView setTranslatesAutoresizingMaskIntoConstraints:false];
[self.childView addSubview:newView];
[self applyConstraintsToParent:self.childView andSubView:newView];
方法
- (void)applyConstraintsToParent:(UIView *)parentView andSubView:(UIView *)subView {
//constraints
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
[parentView addConstraint:leading];
NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
[parentView addConstraint:trailing];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[parentView addConstraint:top];
NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-8];
[parentView addConstraint:bottom];
NSLayoutConstraint *equalWidth = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
[parentView addConstraint:equalWidth];
leading.active = true;
trailing.active = true;
top.active = true;
bottom.active = true;
equalWidth.active = true;
}
#Edit1-子视图约束
#Edit2 -为了更好地理解,我想使用xib(在UIStoryBoard
中正常工作)以编程方式实现此功能.
#Edit2 - For better understanding, I want to achieve this functionality programmatically using xib's(In UIStoryBoard
is just working fine.)
推荐答案
从您的问题中我了解到,您无法在scrollview 1.1中滚动内容.
From your question what i understood is that you are not able to scroll your content in the scrollview 1.1.
尝试以下步骤:
-
对于滚动视图1.1
For the scrollview 1.1
- 使用View1给出顶部,底部,前导,尾随约束.
- 赋予scrollview高度限制= 38
对于子视图UIView 1.2
For the childview UIView 1.2
- 使用scrollview1.1给出顶部,底部,前导,尾随约束
- 使用View1固定子视图,以领先&后缘
- 赋予子视图高度限制
- 赋予子视图垂直居中约束.
对于newView UIView 1.2.1
For the newView UIView 1.2.1
- 从笔尖加载视图.
- 将其添加到子视图
- 设置其约束-顶部,底部,前导&尾随子视图.
这将使您的内容可滚动.
This will make your content scrollable.
我在这里共享了一个示例项目: https://github.com/Abhie87/StackExchangeSample
I have shared a sample project here: https://github.com/Abhie87/StackExchangeSample
希望这会有所帮助.
这篇关于UIStackView:从xib加载视图并更新subView的高度限制没有反映任何变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!