Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust frame of presented view in UIPresentationController after presentation

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?

like image 822
Zev Eisenberg Avatar asked Apr 22 '15 18:04

Zev Eisenberg


1 Answers

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
like image 57
coder.pm Avatar answered Nov 15 '22 13:11

coder.pm