【问题标题】:Editing bounds of UIView when presenting hides keyboard in iOS 8在 iOS 8 中呈现隐藏键盘时编辑 UIView 的边界
【发布时间】:2014-11-12 05:56:49
【问题描述】:

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

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

【问题讨论】:

    标签: ios objective-c ipad uiview keyboard


    【解决方案1】:

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

    编辑: 在 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, 0, 300, 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, 0, displaySize.width, displaySize.height);
    }
    
    @end
    

    关于TransitioningDelegate.h:

    @interface AboutTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>
    
    @end
    

    关于TransitioningDelegate.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

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

    关于PresentationController.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 7 没有包含这个,所以我需要说“如果 iOS 8,使用 UIPresentationController 否则编辑 superview?对吗?似乎不是最理想的。
    • 我遇到了与您类似的问题,并通过这种方式解决了。如果您使用 iOS 8 sdk,那么 viewWillLayoutSubviews 的行为会有所不同,因此我们似乎应该检查系统版本并在 iOS8 中使用 UIPresentationController。
    • 我使用了第一种方法,使用preferedContectSize 属性它按预期工作
    【解决方案2】:

    调用containerViewWillLayoutSubviews()的最简单方法是调用:

    containerView?.setNeedsLayout()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 2019-06-08
      相关资源
      最近更新 更多