Adabtive Autolayout constrain in UICollectionViewHeader

40 views Asked by At

In UICollectionViewHeader this code resizes everything perfectly when the header is on the screen but if I scroll down (to footer for example) and change orientation while footer is on the screen, the header does not update the constrains and no resizing happens.

func observeOrientation() {
    NotificationCenter.default.addObserver(self, selector: #selector(refreshImageConstraints), name: UIDevice.orientationDidChangeNotification, object: nil)
}

@objc func refreshImageConstraints() {
    NSLayoutConstraint.deactivate([headImageWidth, headImageHeight, stackWidth])
    
    if frame.width > frame.height {
        headImageWidth = headImage.widthAnchor.constraint(equalToConstant: frame.width * (6/7))
        headImageHeight = headImage.heightAnchor.constraint(equalToConstant: frame.height * (3/6))
        stackWidth = stack.widthAnchor.constraint(equalToConstant: frame.width * (5/7))
    } else {
        headImageWidth = headImage.widthAnchor.constraint(equalToConstant: frame.width * (6/7) )
        headImageHeight = headImage.heightAnchor.constraint(equalToConstant: frame.width * (3/6))
        stackWidth = stack.widthAnchor.constraint(equalToConstant: frame.width * (6/7))
    }
    NSLayoutConstraint.activate([headImageWidth, headImageHeight, stackWidth])
    print("DEBUG: Did change orientation")
}
0

There are 0 answers