ios - 在 iOS 8 中显示隐藏键盘时编辑 UIView 的边界

标签 ios objective-c ipad uiview keyboard

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

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

这只发生在 iOS 8 中,所以我只能假设它与键盘的呈现方式以及我屏蔽/调整 View 大小的方式有关,但我对解决方案一无所知。

呈现 ViewController -

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

编辑 View 边界 -

- (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 中更改父 View 边界,因为它会导致无限循环。

编辑: 在 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];

编辑 View 边界-

 - (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。看看下面我的代码。

父 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

项目名称前缀.pch

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

关于ios - 在 iOS 8 中显示隐藏键盘时编辑 UIView 的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903412/

相关文章:

ios - UIKit 动态 "SolveVelocityConstraints"崩溃

iphone - iPhone 上的 iAds 插页式广告?

iphone - 移动 x 偏移时的 CGContextDrawImage

objective-c - UIRequiresPersistentWifi——这在 4.2 中仍然可用吗?

iphone - 如何使用 Objective-C 读取 XHTML 文件标签?

ios - xcode 上的短信(有 MessageUI.framework),但仍然失败

iphone - 某些应用程序如何在其应用程序中以编程方式打开设置应用程序

ios - Xcode 4 iOS 项目结构问题

ios - 按秒滚动到顶部到栏项目

objective-c - 带有自定义项的 UIMenuController 不适用于 UICollectionview