在iOS 8中呈现隐藏键盘时编辑UIView的边界

我提出了一个小的“登录”UIViewController作为具有自定义边界的UIModalPresentationFormSheet.在viewWillLayoutSubviews方法中,我将视图的大小更改为(300,250).这在iOS 5/6/7中有效,但在8中不再有效.

当呈现视图并且点击UITextField时,应用程序变得无响应(未冻结,只是没有响应触摸).几乎就像键盘出现但没有出现.委托方法被正确调用.如果我删除self.view.superview.bounds = CGRectMake(0,300,250);从viewWillLayoutSubviews方法键盘工作,但视图现在是一个完整大小的UIModalPresentationFormSheet样式.

这只发生在iOS 8中,所以我只能假设键盘显示的方式以及我屏蔽/调整视图的方式存在问题,但我对解决方案感到茫然.

呈现ViewController –

UserLoginViewController *loginVC = [[UserLoginViewController alloc] initWithNibName:@"UserLoginViewController" bundle:nil];
loginVC.modalPresentationStyle = UIModalPresentationFormSheet;
loginVC.delegate = self;
[self presentViewController:loginVC animated:YES completion:nil];

编辑视图边界 –

- (void)viewWillLayoutSubviews {

    [super viewWillLayoutSubviews];
    self.view.superview.layer.cornerRadius  = 10.0;
    self.view.superview.layer.masksToBounds = YES;
    self.view.superview.bounds = CGRectMake(0,250);
}

解决方法

在iOS8中,您不应更改viewWillLayoutSubviews中的超视图边界,因为它会导致无限循环.

编辑:
在iOS8属性preferredContentSize运行良好.

您应该以这种方式更改代码

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];

编辑视图边界 –

- (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,250);
    }
}

另一种为您提供更多自定义选项的方法是使用UIPresentationController和UIViewControllerTransitioningDelegate.看看下面的代码.

父视图控制器:

_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,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

项目名 – Prefix.pch

#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...