gpt4 book ai didi

ios - UIStackView:从 xib 加载 View 并更新 subview 的高度约束没有反射(reflect)任何变化?

转载 作者:可可西里 更新时间:2023-11-01 04:04:15 24 4
gpt4 key购买 nike

我的应用程序中有以下层次结构

- 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 加载一个 View 并将其添加到 UIView1.2 时,还将高度约束 0 增加到新添加的 subview 的高度,但什么也不会发生。UIView1.2高度没有按预期更新。

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;
}

enter image description here

#Edit1 - subview 约束

ChildView(UIView 1.2)

#Edit2 - 为了更好地理解,我想使用 xib 以编程方式实现此功能(在 UIStoryBoard 中工作正常。)

Working implementation on UIStoryBoard

最佳答案

根据您的问题,我了解到您无法在 scrollview 1.1 中滚动您的内容。

尝试以下步骤:

  1. 对于 ScrollView 1.1

    • 给出顶部、底部、前导、尾随约束 w.r.t View1。
    • 给 scrollview 高度限制 = 38
  2. 对于 subview UIView 1.2

    • 给 top, bottom,leading, trailing constraints w.r.t scrollview1.1
    • 为前缘和后缘固定 subview w.r.t View1
    • 给 subview 高度限制
    • 给 subview 垂直居中约束。
  3. 对于 newView UIView 1.2.1

    • 从 nib 加载 View 。
    • 将其添加到 subview
    • 设置其约束 - 顶部、底部、前导和尾随 w.r.t subview 。

这将使您的内容可滚动。

我在这里分享了一个示例项目:https://github.com/Abhie87/StackExchangeSample

希望这会有所帮助。

关于ios - UIStackView:从 xib 加载 View 并更新 subview 的高度约束没有反射(reflect)任何变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51720099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com