在iOS 8中呈现隐藏键盘时编辑UIView的边界 [英] Editing bounds of UIView when presenting hides keyboard in iOS 8
问题描述
我将一个小的登录 UIViewController
作为带有自定义边界的 UIModalPresentationFormSheet
。在 viewWillLayoutSubviews
方法中,我将视图的大小更改为(300,250)。这在iOS 5/6/7中有效但在8中不再有效。
当显示视图并且 UITextField $ c时点击了$ c>,应用程序变得没有响应(没有冻结,只是没有响应触摸)。几乎就像键盘出现但没有出现。委托方法被正确调用。如果我从
viewWillLayoutSubviews $ c $中删除
self.view.superview.bounds = CGRectMake(0,0,300,250);
c>键盘工作的方法,但视图现在是一个完整大小的 UIModalPresentationFormSheet
样式。
这只发生在iOS 8,所以我只能假设键盘显示的方式以及我屏蔽/调整视图的方式存在问题,但我对解决方案感到茫然。
'p>呈现的ViewController -
UserLoginViewController * loginVC = [[UserLoginViewController的alloc] initWithNibName:@ UserLoginViewController 束:零];
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中的超视图边界,因为它会导致无限循环。
编辑:iOS8属性中的
属性preferredContentSize效果很好。
您应该以这种方式更改代码:
UserLoginViewController * loginVC = [[UserLoginViewController alloc] initWithNibName:@ UserLoginViewControllerbundle: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
#importAboutViewController.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:
'pre>
#进口 AboutTransitioningDelegate.h
#进口 AboutPresentationController.h
@implementation AboutTransitioningDelegate
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController中*)呈现presentingViewController:(UIViewController中*)呈递sourceViewController:(UIViewController中*)源
{
返回[[AboutPresentationController的alloc] initWithPresentedViewController:呈现presentingViewController:呈现];
}
@end
AboutPresentationController.h
#import< UIKit / UIKit.h>
@interface AboutPresentationController:UIPresentationController
@end
AboutPresentationController.m
#importAboutPresentationController.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;
返回r;
}
- (void)containerViewWillLayoutSubviews
{
[super containerViewWillLayoutSubviews];
self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}
@end
ProjectName-Prefix.pch
#define IS_IOS8([[[[UIDevice currentDevice] systemVersion] floatValue]> = 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.
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.
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.
Presenting ViewController -
UserLoginViewController *loginVC = [[UserLoginViewController alloc] initWithNibName:@"UserLoginViewController" bundle:nil];
loginVC.modalPresentationStyle = UIModalPresentationFormSheet;
loginVC.delegate = self;
[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;
self.view.superview.bounds = CGRectMake(0, 0, 300, 250);
}
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)
这篇关于在iOS 8中呈现隐藏键盘时编辑UIView的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!