UIAlertController is moved to buggy position at top of screen when it calls `presentViewController:`

前端 未结 12 1371
心在旅途
心在旅途 2020-12-08 06:36

Presenting a view from a UIAlertController moves the alert to a buggy position at the top-left corner of the screen. iOS 8.1, device and simulator.

We h

12条回答
  •  眼角桃花
    2020-12-08 07:25

    User manoj.agg posted this answer to the Open Radar bug report, but says:

    Somehow I don't have enough reputation to post answers on Stackoverflow.

    Posting his answer here for posterity. I have not tested/evaluated it.


    Step 1:

    Create a custom View Controller inheriting from UIViewController and implement UIPopoverPresentationControllerDelegate:

    @interface CustomUIViewController : UIViewController
    

    Step 2:

    Present the view in fullscreen, making use of the presentation popover:

    CustomUIViewController *viewController = [[CustomUIViewController alloc] init];
    viewController.view.backgroundColor = self.view.tintColor;
    viewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
    
    UIPopoverPresentationController *popController = viewController.popoverPresentationController;
    popController.delegate = viewController;
    
    [alert presentViewController:viewController animated:YES completion:^{
        dispatch_after(0, dispatch_get_main_queue(), ^{
            [viewController dismissViewControllerAnimated:YES completion:nil];
        });
    }];
    

    I had a similar problem where a password input view needed to be displayed on top of any other View Controller, including UIAlertControllers. The above code helped me in solving the problem. Noteworthy change in iOS 8 is that UIAlertController inherits from UIViewController, which was not the case for UIAlertView.

提交回复
热议问题