简体   繁体   中英

Detect device rotation inside a collectionViewCell

Inside a custom collectionVirewCell I create a gradient layer and add it to the cell in the function override func awakeFromNib() {}

 self.layer.insertSublayer(gradient, at: 0)

When the device rotates the size of the cell changes but not the size of the layer. I need to do something like

override func viewDidLayoutSubviews() {
    gradient.frame = self.bounds

}

The problem is that I get an error telling me that viewDidLayoutSubviews() did not override any function from the superclass. Is there any method to detect the cell size variation inside the cell class?

viewDidLayoutSubviews() is a UIViewController method and not a method of UIView which UICollectionViewCell inherits from.

The method you're looking to override is layoutSubviews()

override func layoutSubviews() {
    <#code#>
}

I think that's your problem!

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