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?

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • 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? – Mike Jul 16 '15 at 00:28
  • 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? – Zev Eisenberg Jul 16 '15 at 00:32
  • 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 :) – Mike Jul 16 '15 at 01:24
  • Agreed. This would make a good WWDC labs question. – Zev Eisenberg Jul 16 '15 at 02:36
  • 1
    Update: I still don't have a better workaround than the one described here. – Zev Eisenberg Apr 18 '17 at 19:58

1 Answers1

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
coder.pm
  • 71
  • 2
  • 8