ImagePickerSheetController icon indicating copy to clipboard operation
ImagePickerSheetController copied to clipboard

Background view wrong frame when inside popover

Open fruitcoder opened this issue 8 years ago • 1 comments

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)
    }

...

fruitcoder avatar May 13 '16 16:05 fruitcoder

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

fruitcoder avatar May 13 '16 16:05 fruitcoder