'modalPresentationStyle not being set to pageSheet in segue

I have 3 view controllers lets says ControllerA, ControllerB and ControllerC. ControllerA and ControllerB both have a button on it and when clicked it opens ControllerC.

ControllerA and ControllerB could be displayed in half screen-height size/half size and it can also be pulled up to be full screen size. I want ControllerC's size to be same as ControllerA and ControllerB size when opened i.e. if the presenting controller is half size then ControllerC should also be half size and if presenting controller is full screen then ControllerC should be full screen too.

The way I am opening ControllerC from ControllerA and ControllerB is different. The code that I added in ControllerA works fine for what I want to achieve, but the code that I add in ControllerB does not work i.e. it always opens ControllerC in full screen mode.

ControllerA Code(works fine i.e. opens half screen/full screen depending on ControllerA's size):

let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controllerC = storyboard.instantiateViewController(withIdentifier: "ControllerC") as? ControllerC,
   let hostVC = myPresentingController {
   controllerC.modalPresentationStyle = .pageSheet
   controllerC.transitioningDelegate = hostVC
   controllerC.delegate = self
   controllerC.willMove(toParent: hostVC)
   hostVC.view.addSubview(controllerC.view)
   hostVC.addChild(controllerC)
   controllerC.didMove(toParent: hostVC)
}

ControllerB code(present ControllerC via segue. Adding modalPresentationStyle as pageSheet always opens the view in full screen... Looking for a fix for this code)

if segue.identifier == "segueToControllerC", let controllerC = segue.destination as? ControllerC {
   controllerC.delegate = self
   controllerC.selectedDate = selectedDate
   controllerC.modalPresentationStyle = .pageSheet
}

Other fix that I tried to open ControllerC from ControllerB(this does not work as well.. ControllerC always opens in fullscreen with this fix too)

if segue.identifier == "segueToControllerC", let controllerC = segue.destination as? ControllerC {
   controllerC.delegate = self
   controllerC.selectedDate = selectedDate
   controllerC.view.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.width, height: self.view.frame.height)
   controllerC.view.translatesAutoresizingMaskIntoConstraints = false
}

How do I open ControllerC from ControllerB in the same size as ControllerB i.e. without resizing ControllerC to fullscreen?

This is how ControllerC is set in Storyboard

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source