Editing bounds of UIView when presenting hides keyboard in iOS 8

后端 未结 2 1010
再見小時候
再見小時候 2020-12-21 06:50

I present a small \"login\" UIViewController as a UIModalPresentationFormSheet with custom bounds. In the viewWillLayoutSubviews metho

相关标签:
2条回答
  • 2020-12-21 07:15

    The simplest way to call containerViewWillLayoutSubviews() is to call:

    containerView?.setNeedsLayout()
    
    0 讨论(0)
  • 2020-12-21 07:21

    In iOS8 You shouldn't change superview bounds in viewWillLayoutSubviews because it causes infinite loop.

    EDIT: in iOS8 property preferredContentSize works well.

    You should change your code in that way:

      UserLoginViewController *loginVC = [[UserLoginViewController alloc] initWithNibName:@"UserLoginViewController" bundle:nil];
        loginVC.modalPresentationStyle = UIModalPresentationFormSheet;
        loginVC.delegate = self;
        if(IS_IOS8)
        {
            loginVC.preferredContentSize = CGSizeMake(300, 250);
        }
        [self presentViewController:loginVC animated:YES completion:nil];
    

    Editing the view bounds -

     - (void)viewWillLayoutSubviews{
        [super viewWillLayoutSubviews];
    
        self.view.superview.layer.cornerRadius  = 10.0;
        self.view.superview.layer.masksToBounds = YES;
    
        if(!IS_IOS8)
        {
            self.view.superview.bounds = CGRectMake(0, 0, 300, 250);
        }
    }
    

    Another way, which gives you more customization options is to use UIPresentationController and UIViewControllerTransitioningDelegate. Take a look at my code below.

    Parent view controller:

     _aboutViewController = [[AboutViewController alloc] init];
            _aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
            if(IS_IOS8)
            {
                if(aboutTransitioningDelegate == nil)
                {
                    aboutTransitioningDelegate = [[AboutTransitioningDelegate alloc] init];
                }
                _aboutViewController.transitioningDelegate = aboutTransitioningDelegate;
                _aboutViewController.modalPresentationStyle = UIModalPresentationCustom;
            }
            [self presentViewController:_aboutViewController animated:YES completion:nil];
    

    AboutViewController.m

    #import "AboutViewController.h"
    
    @interface AboutViewController ()
    
    @end
    
    @implementation AboutViewController
    
    - (void)viewWillLayoutSubviews{
        [super viewWillLayoutSubviews];
    
        if(IS_IOS8)
        {
            return;
        }
        CGSize displaySize = CGSizeMake(320, 462);
    
        self.view.superview.bounds = CGRectMake(0, 0, displaySize.width, displaySize.height);
    }
    
    @end
    

    AboutTransitioningDelegate.h:

    @interface AboutTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>
    
    @end
    

    AboutTransitioningDelegate.m:

    #import "AboutTransitioningDelegate.h"
    #import "AboutPresentationController.h"
    @implementation AboutTransitioningDelegate
    
    -(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
    {
        return [[AboutPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    }
    @end
    

    AboutPresentationController.h

    #import <UIKit/UIKit.h>
    
    @interface AboutPresentationController : UIPresentationController
    
    @end
    

    AboutPresentationController.m

    #import "AboutPresentationController.h"
    
    @implementation AboutPresentationController
    
    
    -(CGRect)frameOfPresentedViewInContainerView
    {
        CGSize displaySize = CGSizeMake(320, 462);
    
        if([[Config sharedInstance] latestVersionFromAppstoreInstalled])
        {
            displaySize = CGSizeMake(320, 416);
        }
    
        CGRect  r =  CGRectZero;
        r.size = displaySize;
        r.origin.y = self.containerView.bounds.size.height/2 - displaySize.height/2;
        r.origin.x = self.containerView.bounds.size.width/2 - displaySize.width/2;
        return r;
    
    }
    -(void)containerViewWillLayoutSubviews
    {
        [super containerViewWillLayoutSubviews];
        self.presentedView.frame = [self frameOfPresentedViewInContainerView];
    }
    
    @end
    

    ProjectName-Prefix.pch

    #define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
    
    0 讨论(0)
提交回复
热议问题