UIStackView: Loading views from xib and updating height constraint of subView did not reflecting any changes?

…衆ロ難τιáo~ 提交于 2019-12-01 17:25:16

From your question what i understood is that you are not able to scroll your content in the scrollview 1.1.

Try the following steps :

  1. For the scrollview 1.1

    • give top, bottom,leading, trailing constraints w.r.t View1.
    • give scrollview height constraint = 38
  2. For the childview UIView 1.2

    • give top, bottom,leading, trailing constraints w.r.t scrollview1.1
    • pin childview w.r.t View1 for leading & trailing edges
    • give childview height constraints
    • give childview vertically center constraint.
  3. For the newView UIView 1.2.1

    • Load view from nib.
    • Add it to the childview
    • Set its constraints - top, bottom, leading & trailing w.r.t childview.

This will make your content scrollable.

I have shared a sample project here: https://github.com/Abhie87/StackExchangeSample

Hope this will be helpful.

Tried to do the same and it worked as expected. What I did:

  • Initial views hierarchy looks like this:

  • Stack view constraints are on the next image:

  • Each view in stack view has only its' height set by constraint (the button in last view I use to add/remove the new view to the stack view at index 0)
  • The view to add is loaded from .xib file called SomeView.xib view hierarchy and constraints for which are on the next image (constraint called View Height Constraint is set to 1 and is changed to 95 when SomeView is added to the stack view):

The function that is called when the button is tapped looks like this:

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

Hope this will give you any suggestions in how to fix your situation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!