Reputation: 2484
I have a UICollectionViewController with a UICollectionReusableView (header). In this UICollectionReusableView, I have a subview (in pink).
I have set some constraints to give to the subview, a full screen width.
It's working perfectly for the background img (in black). However, concerning the subview, it's working only with the iPhone 6 dimensions (it's too large for iPhone 5s and not enough for iPhone 6 plus for example). I really don't know why it's working for the background image and not for this subview. The pink subview is "Radius View on Top" and the image in the background is "Ban img".
I have tried to code this in the UICollectionReusableView's awakeFromNib()
let width = UIScreen.main.bounds.size.width
self.widthConstraintRadiusView.constant = width
I have found something:
I can see that the subview has the right width, but it's not stretched? How is it possible? What do I have to understand?
If I use the function roundCorners on the subview, it's not working:
func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
radiusViewOnTop.roundCorners([.topLeft,.topRight], radius: 7)
How could I keep the function and have the right width?
Upvotes: 0
Views: 424
Reputation: 19892
With your latest edit I know why it's not working, and it's a simple fix.
Your view is being sized properly, but the mask you are adding is the wrong size.
You designed your interface with the iPhone 6 size, and those are the dimensions the view is loaded with. After that it performs a layout pass on your view to update the size to whatever the actual size should be.
However, you are probably calling the round corners method on awakeFromNib
or something similar, which happens after the view is loaded but before the size is updated.
The fix is simple, override layoutSubviews
, call super
first and then call the roundCorners:
method.
Upvotes: 1