简体   繁体   中英

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++.

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

在此处输入图片说明

after 2 seconds of viewDidLoad

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM