【问题标题】:UIStackView: Loading views from xib and updating height constraint of subView did not reflecting any changes?UIStackView:从 xib 加载视图和更新 subView 的高度约束没有反映任何变化?
【发布时间】:2019-01-14 03:35:13
【问题描述】:

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

- 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.2height 没有按预期更新。

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 中工作正常。)

【问题讨论】:

  • 仍然没有提到所有的约束。你有连接子视图和它的超级视图的底部约束吗?您的代码中的self 是什么? (现在可以说你绝对不需要equalWidth 约束,因为根据前导和尾随约束,宽度已经必须相等)
  • 好的,我将放置另一个图像作为子视图约束,self 是添加到 UIStackView 的 UIView 1。你能看看我的视图层次结构吗?
  • @AntonFilimonov 请查看我更新的问题。
  • 试图做同样的事情 - 一切都按预期工作。你在控制台中收到任何警告吗?可能有些约束存在冲突。
  • 不,没有一个警告。但在我的情况下它不起作用。

标签: ios objective-c uiscrollview uistackview


【解决方案1】:

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

尝试以下步骤:

  1. 对于滚动视图 1.1

    • 根据 View1 给出顶部、底部、前导、尾随约束。
    • 给滚动视图高度约束 = 38
  2. 对于子视图 UIView 1.2

    • 给出顶部、底部、前导、尾随约束 w.r.t scrollview1.1
    • pin childview w.r.t View1 用于前缘和后缘
    • 给子视图高度限制
    • 给子视图垂直居中约束。
  3. 对于newView UIView 1.2.1

    • 从笔尖加载视图。
    • 将其添加到子视图中
    • 设置其约束 - 顶部、底部、前导和尾随 w.r.t 子视图。

这将使您的内容可滚动。

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

希望这会有所帮助。

【讨论】:

  • 无法在“...Downloads/StackExchangeSample-master/StachExchangeBounty.xcodeproj”加载项目,项目版本不兼容。哪个版本的 Xcode?​​span>
  • 好吧,让我试试。但如果您能看到我的问题,我已经将其作为您的解决方案。
  • 我已经编辑了我的答案。你可以试试这个小改变
  • 我看到你的实现有点混乱。为什么要在 UIScrollView 中添加 childView。默认情况下,它有一个父滚动视图。所以我只想在子视图高度发生变化时增加childView高度。
  • 您还提供了一个固定的高度约束 200。为什么?
【解决方案2】:

尝试做同样的事情,它按预期工作。 我做了什么:

  • 初始视图层次结构如下所示:
  • 堆栈视图约束在下一个图像上:
  • 堆栈视图中的每个视图仅具有由约束设置的高度(最后一个视图中的按钮,我用于将新视图添加/删除到索引 0 处的堆栈视图)
  • 要添加的视图是从名为 SomeView.xib 的视图层次结构和约束位于下一个图像上的 .xib 文件加载的(名为 View Height Constraint 的约束设置为 1,并在添加 SomeView 时更改为 95到堆栈视图):

点击按钮时调用的函数如下所示:

- (IBAction)buttonTap:(UIButton *)sender {
    if (self.theView) {
        [self.stackView removeArrangedSubview:self.theView];
        [self.theView removeFromSuperview];
        self.theView = nil;
    } else {
        self.theView = [[[NSBundle mainBundle] loadNibNamed:@"SomeView" owner:nil options:nil] firstObject];
        self.theView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.stackView addSubview:self.theView];
        [self.stackView insertArrangedSubview:self.theView atIndex:0];
        self.theView.viewHeightConstraint.constant = 95;
    }
}

希望这会给你任何关于如何解决你的情况的建议

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 2017-09-27
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多