UIPopOverController shrinking when presenting alert controller

1k views Asked by At

I have the following UIPopOverController on iPad. This is an iOS 8 app with universal storyboards. In XCode, I have selected the segue of this as "Present as Popover".

enter image description here

Whenever this view controller presents a UIAlertController, this happens:

enter image description here

The popover shrinks to a weird size. The UIAlertController is presented from the pop over as:

        var alert = UIAlertController(title: NSLocalizedString("Error", comment: "A simple error label"), message: NSLocalizedString("This account is already linked to the app.", comment: "A string describing the problem"), preferredStyle: .Alert)
        var action = UIAlertAction(title: NSLocalizedString("OK", comment: "Simple string"), style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
            self.usernameTextField.becomeFirstResponder()
            self.passwordTextField.text = ""
            return
        })
        alert.addAction(action)
        self.presentViewController(alert, animated: true, completion: nil)

I have not played with the constraints of the view controllers at all, so I Have no idea why this is happening. What's the correct way to prevent this from happening?

4

There are 4 answers

0
Andy Ibanez On BEST ANSWER

It looks like this is a bug with iOS 8.0.2 and below. All my devices are updated to iOS 8.1 now. I haven't changed my code and all my popovers don't shrink when they present anymore.

0
Noiibe On

The problem occurs when you present the UIViewController with modalPresentationStyle = UIModalPresentationStyle.CurrentContext and show an alert with UIAlertController. Use UIModalPresentationStyle.OverCurrentContext instead.

0
Ashvin On

Its working for me. nav.preferredContentSize =CGSizeMake(320, 480); My code is below, may be help you.

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myController];
nav.preferredContentSize =CGSizeMake(320, 480);
popOver = [[UIPopoverController alloc] initWithContentViewController:nav];
[popOver presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:NO];
0
Victor Kononov On

Maybe it is too late now, but I've occurred same issue on iOS9.0. I have a UINavigationController controller with root UIViewController placed inside UIPopoverController. After presenting UIAlertController from UIViewController the popover shrinks to alert view size. After some researching I came with this solution:

- (void)viewWillLayoutSubviews
{
    CGSize preferredContentSize = self.preferredContentSize;
    CGSize fakeMomentarySize    = CGSizeMake(preferredContentSize.width + 1.0f, preferredContentSize.height + 1.0f);
    self.preferredContentSize   = fakeMomentarySize;
    self.preferredContentSize   = preferredContentSize;

    CGRect frame    = self.view.frame;
    frame.size      = [self preferredContentSize];
    self.view.frame = frame;

    [super viewWillLayoutSubviews];
}

I've put this code in my UIViewController. For some reason after dismissing alert view no viewWillAppear or viewDidAppear methods are called and navigation controller delegate also doesn't reply to this move so view controller doesn't take part in calculating it's size. So I've put it in viewWillLayoutSubviews. The magic with setting preferredContentSize is described in this question. Also I needed to restore view frame because it still got shrunk. UIViewController preferredContentSize should be set elsewhere earlier.

Hope this helps someone.