隐藏键盘时Blackview来了

Black view is comming while hiding a keyboard

这是我为在用户开始在 textview 中输入时移动 textview 编写的一些代码。

这是代码,

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if(textView == _textViewMsg || textView == _subjectView ){
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
   // return YES;
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if(textView == _textViewMsg || textView == _subjectView ){
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [self.view endEditing:YES];
    }
    return YES;
}


- (void)keyboardDidShow:(NSNotification *)notification
{

    [self.view setFrame:CGRectMake(0,-50,320,460)];
}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self.view setFrame:CGRectMake(0,60,320,520)];
}

当我在移动中单击 textview(在主题中)视图时,我可以在 textview 中键入内容。它工作正常。 (图 1)

当我点击完成按钮时,同时隐藏,即在键盘隐藏时,一个黑色视图出现(检查图像 2)几秒钟,然后再次出现正常视图。

编辑:

解决方案 : @michael 刚刚给了我解决方案

UIKeyboardDidHideNotification 更改为 UIKeyboardWillHideNotification 它对我有用。

出现新问题: 当我开始输入前 2 个文本视图时,即请求者、名字和姓氏...我无法输入,因为它正在向上移动。

添加观察者应该在 viewdidload 中。尝试将添加键盘观察者的代码移动到 viewdidload。

编辑

而不是 did... 通知类型检查 will... 所以用 UIKeyboardWillShow 替换 UIKeyboardDidShowNotification,用 UIKeyboardWillHide 替换 UIKeyboardDidHideNotification。

使用 IQKeyboardManager 框架将有助于处理键盘隐藏和取消隐藏问题,

最有效的键盘处理方式:IQKeyboardManager

方法一: 您可以为 IQKeyboardManager 库使用 Cocoa Pod 文件。

  1. 初始化 pod 文件。
  2. 豆荚'IQKeyboardManager'
  3. 安装 Pod 文件。

方法二:

从 github 下载 IQKeyboardManger SDK 并拖放到您的项目文件中。

Swift 代码片段:

导入 IQKeyboardManagerSwift

@UIApplicationMain class AppDelegate:UIResponder,UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  IQKeyboardManager.sharedManager().enable = true

  return true
}

}

Objective-C 片段:

#import <IQKeyboardManager/IQKeyboardManager.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //ONE LINE OF CODE.
    //Enabling keyboard manager(Use this line to enable managing distance between keyboard & textField/textView).
    [[IQKeyboardManager sharedManager] setEnable:YES];
return true;
}

希望这能帮助您解决键盘隐藏和取消隐藏 UI 问题。

这是因为当系统动画完成时,您在键盘消失后更改了视图的框架。快速修复:将 UIKeyboardDidHideNotification 更改为 UIKeyboardWillHideNotification

但如果您不介意的话,我想指出您代码中的一些其他项目。

1: 每次用户开始编辑时,您都在订阅通知。这意味着当用户第二次开始输入时,您的代码将被触发两次。在 viewDidAppear 上订阅通知和在 viewWillDisappear 上取消订阅更合适:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

2: 然后您将框架更改为恒定值。但现在苹果支持不同尺寸的不同键盘,更不用说键盘尺寸在不同设备和不同可能模式下有所不同。所以从通知中获取键盘的大小更明智:

- (void)keyboardDidShow:(NSNotification *)aNotification
{
    CGRect keyboardFrame = [[aNotification.userInfo valueForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect overlap =  CGRectIntersection( self.view.frame, keyboardFrame);
    // Setup new frmae according to overlap, for example reduce size by half of overlap, and move up by another half
    CGRect newFrame = self.view.frame;
    newFrame.origin.y -= overlap.size.height / 2.0
    newFrame.size.height -= overlap.size.height / 2.0
    self.view.frame = newFrame;
}

3: 动画:您也可以从通知中获取键盘动画持续时间: UIKeyboardAnimationDurationUserInfoKey:

NSTimeInterval duration = [[aNotification.userInfo  valueForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
    self.view.frame = newFrame;
}];

4 由于您似乎在使用滚动视图(UITableView 和 UICollectioView 是滚动视图),因此您可以不更改框架,而是更改内容插入(将空 space 添加到底部滚动视图)

[self.view setContentInset:UIEdgeInsetsMake(0, 0, overlap.size.height, 0)];

5 考虑使用自动布局。