iPad custom size of modal view controller

前端 未结 13 1972
挽巷
挽巷 2020-12-07 08:11

I have a couple of modal view controllers of certain size. I\'m trying to avoid the use of custom views (creating full screen black translucent overlay over current view, ad

13条回答
  •  既然无缘
    2020-12-07 08:50

    This is my solution for a NOT autolayout view (never tried with autolayout) and compliant with iOS 7 and iOS 8.

    At first be sure to call the modal view in this way:

    CloudSettingsViewController *cloudController = [[CloudSettingsViewController alloc] initWithNibName:@"CloudSettingsView" bundle:nil];
    
    CGRect originalBounds = cloudController.view.bounds;
    
    cloudController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    cloudController.modalPresentationStyle = UIModalPresentationFormSheet;
    
    [self presentViewController:cloudController animated:YES completion:nil]; 
    

    With iOS 8 set the preferredContentSize in the viewDidLoad method of the modal view.

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // backup of the original size (selected in interface builder)
        height = self.view.frame.size.height;
        width = self.view.frame.size.width;
    
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) // versioni successive ios 8
            self.preferredContentSize = CGSizeMake(width, height); // this is necessary to get the correct size of the modal view
    }
    

    This works ALSO when the modal view needs to display the keyboard on iPad.

    With previous versions of iOS 8 you should set the bounds after presentViewController call:

    [currentController presentViewController:controlPanelController animated:YES completion:nil];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8) 
        cloudController.view.superview.bounds = originalBounds;//it's important to do this after 
    

提交回复
热议问题