UIStackView:从xib加载视图和更新subView的高度约束没有反映任何变化?

问题描述 投票:6回答:2

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

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

enter image description here

#Edit1 - 子视图约束

ChildView(UIView 1.2)

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

Working implementation on UIStoryBoard

ios objective-c uiscrollview uistackview
2个回答
-1
投票

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

请尝试以下步骤:

  1. 对于scrollview 1.1 给出top,bottom,leading,trailing约束w.r.t View1。 给scrollview高度约束= 38
  2. 对于子视图UIView 1.2 给出top,bottom,leading,trailing约束w.r.t scrollview1.1 pin childview w.r.t View1用于前缘和后缘 给出childview高度限制 给childview垂直中心约束。
  3. 对于newView UIView 1.2.1 从笔尖加载视图。 将其添加到子视图 设置其约束 - 顶部,底部,前导和尾随w.r.t子视图。

这将使您的内容可滚动。

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

希望这会有所帮助。


-2
投票

试图做同样的事情并按预期工作。我做了什么:

  • 初始视图层次结构如下所示:enter image description here
  • 堆栈视图约束在下一个图像上:enter image description here
  • 堆栈视图中的每个视图都只有'约束高度设置(我在上一个视图中使用的按钮,用于在索引0处添加/删除新视图到堆栈视图)
  • 要添加的视图是从.xib文件中加载的,该文件名为SomeView.xib视图层次结构,其约束位于下一个图像上(约束称为View Height Constraint设置为1,当SomeView添加到堆栈视图时更改为95):enter image description here

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.