Adjust frame of presented view in UIPresentationController after presentation
Asked Answered
P

1

16

I'm using a custom presentation controller to make something similar to UIAlertView. I want it to move up when the keyboard shows. In the UIKeyboardWillShowNotification handler, I'm grabbing the new keyboard frame, storing it in a property, and then calling:

self.presentedView.frame = [wself frameOfPresentedViewInContainerView];

In my implementation of -frameOfPresentedViewInContainerView, I take the current keyboard height into account.

This works perfectly, but it feels a little dirty to be modifying the frame of self.presentedView directly. I tried triggering layout on the container view with various permutations of setNeedsLayout and layoutIfNeeded, but nothing causes the presented view to move other than just setting its frame directly.

Is there a better way to do this than to just change the frame myself?

Plast answered 22/4, 2015 at 18:7 Comment(5)
Hi Zev, did you manager to find a solution? I'm trying to update the frame of my presentedView on a button press... is this something similar to what you're doing?Eliathan
I didn’t find a better solution. Just stuck with the workaround. Are you also in a presentation controller, or just adjusting a view in general?Plast
also in a presentation controller, I wanted to adjust the frame when the user takes an action.. ended up modifying presentedView()? directly.. doesn't feel right :)Eliathan
Agreed. This would make a good WWDC labs question.Plast
Update: I still don't have a better workaround than the one described here.Plast
K
5

You need to update the preferredContentSize of your presented VC.

The presentation controller does not know how and when the layout of the presented VC will change after the presentation finishes. It only knows it's size and the position needed to perform the transition.

But the presentation controller has a nice delegate method inherited from UIContentContainer: preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer)

So what you should do is:

  1. in the presented VC perform all your layout changes
  2. make sure to update the presented VC preferredContentSize right after
  3. your presentation controller will be notified
  4. trigger a layout pass of the containerView property of your presentation controller
Kuroshio answered 5/4, 2018 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.