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

Editing bounds of UIView when presenting hides keyboard in iOS 8(在 iOS 8 中呈现隐藏键盘时编辑 UIView 的边界)
本文介绍了在 iOS 8 中呈现隐藏键盘时编辑 UIView 的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I present a small "login" UIViewController as a UIModalPresentationFormSheet with custom bounds. In the viewWillLayoutSubviews method, I change the size of the view to (300,250). This has worked in iOS 5/6/7 but no longer works in 8.

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

When the view is presented and a UITextField is tapped, the app becomes unresponsive (not frozen, just not responding to touches). Almost as if the keyboard is presented but not appearing. Delegate methods ARE called correctly. If I remove the self.view.superview.bounds = CGRectMake(0, 0, 300, 250); from the viewWillLayoutSubviews method the keyboard works, but the view is now a full sized UIModalPresentationFormSheet style.

这只发生在 iOS 8 中,所以我只能假设它是键盘呈现方式以及我屏蔽/调整视图大小的方式的问题,但我不知道解决方案.

This only happens in iOS 8, so I can only assume its an issue with the way the keyboard is presented and the way I am masking/resizing the view, but I'm at a loss as to the solution.

呈现 ViewController -

Presenting 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, 0, 300, 250);
}

推荐答案

在 iOS8 中你不应该在 viewWillLayoutSubviews 中改变 superview 的边界,因为它会导致无限循环.

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

在 iOS8 属性中,preferredContentSize 效果很好.

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

编辑视图边界 -

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

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

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

父视图控制器:

 _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

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

关于TransitioningDelegate.h:

AboutTransitioningDelegate.h:

@interface AboutTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>

@end

关于TransitioningDelegate.m:

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

关于PresentationController.h

AboutPresentationController.h

#import <UIKit/UIKit.h>

@interface AboutPresentationController : UIPresentationController

@end

关于PresentationController.m

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

项目名称-前缀.pch

ProjectName-Prefix.pch

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

这篇关于在 iOS 8 中呈现隐藏键盘时编辑 UIView 的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How to mimic Keyboard animation on iOS 7 to add quot;Donequot; button to numeric keyboard?(如何在 iOS 7 上模拟键盘动画以添加“完成数字键盘的按钮?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
How do I retrieve keystrokes from a custom keyboard on an iOS app?(如何从 iOS 应用程序上的自定义键盘检索击键?)