I’m trying to present a custom bottom sheet with a .custom modalPresentationStyle from another VC.
I implemented this custom presentation controller:
class BaseSheetPresentationController: UIPresentationController {
var height: CGFloat = 0
init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?, height: CGFloat = 388.0) {
self.height = height
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
}
private let dimmingView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
view.alpha = 0.0
return view
}()
override var frameOfPresentedViewInContainerView: CGRect {
guard let containerView = containerView else {
return .zero
}
let yOffset = containerView.bounds.height - height
return CGRect(x: 0, y: yOffset, width: containerView.bounds.width, height: height)
}
override func presentationTransitionWillBegin() {
guard let containerView = containerView else { return }
dimmingView.frame = containerView.bounds
containerView.addSubview(dimmingView)
dimmingView.addSubview(presentedViewController.view)
guard let coordinator = presentedViewController.transitionCoordinator else {
dimmingView.alpha = 1.0
return
}
coordinator.animate(alongsideTransition: { _ in
self.dimmingView.alpha = 1.0
})
}
override func dismissalTransitionWillBegin() {
guard let coordinator = presentedViewController.transitionCoordinator else {
dimmingView.alpha = 0.0
return
}
coordinator.animate(alongsideTransition: { _ in
self.dimmingView.alpha = 0.0
})
}
override func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews()
presentedView?.frame = frameOfPresentedViewInContainerView
presentedView?.backgroundColor = .white
}
}
but i don’t know why it appears like this
But if we see the main view in the layout inspector:
Apparently the main view has the width and height correct (388 points) as we can see in the inspector but the white background has not grow to that height and width.
What i’m doing wrong? I have to say iOS seems less clever than Android in building views.
Tried to ask GPT and force the frame of the presented viewcontroller view inside willPresent method. I’m expecting that white background of main view has the same height and width than the view is applying to.