ImagePickerSheetController
ImagePickerSheetController copied to clipboard
Background view wrong frame when inside popover
When I use the sheet inside a popover, the background view has a wrong frame. Looking into viewDidLayoutSubviews, the view of the ImagePickerSheetController is in fact wrong (as is the enclosing popoverView). To fix this I added two lines of code inside viewDidLayoutSubviews: ... let sheetSize = CGSize(width: view.bounds.width, height: sheetHeight)
if let _ = popoverPresentationController {
backgroundView.frame = CGRect(origin: .zero, size: sheetSize)
}
...
I actually changed a little more because the arrow color didn't match the background color.
I added a new public property which changes the backgroundColor based on the environment:
public var backgroundColor: UIColor = UIColor(white: 0.0, alpha: 0.3961) {
didSet {
if let popoverController = popoverPresentationController {
popoverController.backgroundColor = backgroundColor
backgroundView.backgroundColor = .clearColor
} else {
backgroundView.backgroundColor = backgroundColor
}
}
}
lazy var backgroundView: UIView = {
let view = UIView()
view.accessibilityIdentifier = "ImagePickerSheetBackground"
view.backgroundColor = self.backgroundColor
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ImagePickerSheetController.cancel)))
return view
}()
In viewDidLayoutSubviews()
after sheetSize
is calculated
if let controller = popoverPresentationController {
controller.backgroundColor = backgroundColor
backgroundView.backgroundColor = .clearColor()
backgroundView.frame = CGRect(origin: .zero, size: sheetSize)
}
This code might be better of in willMoveToParentViewController
or something but this fits my needs