Semny
Semny

Reputation: 11

Use UIStackview in xib, Hiden and show subview issue

Use UIStackview in xib, Hiden and show subview issue I have two subviews in my StackView, A subview is hidden, B subview is unhidden,A is on the top of B;Set the status of A to show,But A is covered by B.

only occurs in the Xcode10.1 and ios12.0++.

Upvotes: 0

Views: 83

Answers (2)

Semny
Semny

Reputation: 11

I found the reason, because the height constraint of the A view is not effective when the constant property of the heightLayoutConstraint is changed from 0 to the actual height of 100. These are all done in the main thread.

UICollectionReusableView *customView;//Xib init
NSLayoutConstraint *customHeightConstraint;
//change constant
customHeightConstraint.constant = height;
[customView updateConstraints];

But I looked at the effects and properties in the debug view hierarchy and found that the customView is still the initial value of 0.

Upvotes: 0

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4409

I wondered why it is happening only to you..!! However, I've tried the same way that what you specified in the question and got the result perfectly.

Code:

override func viewDidLoad() {
    super.viewDidLoad()

    let myStackView = UIStackView()
    myStackView.axis = .vertical
    myStackView.alignment = .center
    myStackView.distribution = .fillEqually
    myStackView.spacing = 8
    view.addSubview(myStackView)

    let redView = UIView()
    redView.backgroundColor = UIColor.red
    redView.widthAnchor.constraint(equalToConstant: 125.0).isActive = true
    redView.heightAnchor.constraint(equalToConstant: 50).isActive = true

    let greenView = UIView()
    greenView.backgroundColor = UIColor.green
    greenView.widthAnchor.constraint(equalToConstant: 125.0).isActive = true
    greenView.heightAnchor.constraint(equalToConstant: 50).isActive = true

    myStackView.addArrangedSubview(redView)
    myStackView.addArrangedSubview(greenView)

    myStackView.translatesAutoresizingMaskIntoConstraints = false
    myStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0).isActive = true
    myStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0.0).isActive = true


    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        redView.isHidden = true
    }

    DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
        redView.isHidden = false
    }
}

Screenshots:

On viewDidLoad & and after 4 seconds of viewDidLoad

enter image description here

after 2 seconds of viewDidLoad

enter image description here

Upvotes: 1

Related Questions