关于UIAlertView键盘弹起的问题

这里记录一下我在开发中遇到的一个奇葩的问题,经过百度后我发现这个问题确实存在,也有不少人遇到过,今天就把它写出来。其实我们单纯的使用UIAlertView是没什么问题的,但是当UI中有输入框时,当输入完成后,我们点击按钮弹出UIAlertView的时候,在点击UIAlertView上面的按钮跳转到下一个页面的时候,那么问题就来了,在后一个页面会弹出键盘。这个问题刚开始出现的时候,莫名奇妙,也找了好久,因为在模拟器上是不容易察觉的,在真机上就非常明显了。

  • 下面我们模拟一下这个现象,在此来给大家加深印象,避免跟我一样遇到这个问题而纠结了。直接上代码。
  • #import "ViewController.h"
    #import "ViewControllerOne.h"
    
    @interface ViewController ()<UIAlertViewDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
        button.center = self.view.center;
        [self.view addSubview:button];
        [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
        textField.backgroundColor = [UIColor redColor];
        [self.view addSubview:textField];
        [self obserKeyboard];
    }
    
    #pragma mark - 键盘的监控
    - (void)obserKeyboard {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardDidHideNotification object:nil];
    }
    
    - (void)click {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"测试UIAlertView" message:@"弊端" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"好玩么", nil];
        [alertView show];
    }
    #pragma mark 键盘动作
    - (void)keyboardWillShow:(NSNotification *)notif {
        NSLog(@"键盘将要弹出-keyboardWillShow");
    }
    - (void)keyboardShow:(NSNotification *)notif {
        NSLog(@"键盘已经弹出-keyboardShow");
    }
    - (void)keyboardWillHide:(NSNotification *)notif {
        NSLog(@"键盘将要隐藏-keyboardWillHide");
    }
    - (void)keyboardHide:(NSNotification *)notif {
        NSLog(@"键盘已经隐藏-keyboardWillHide");
    
    }
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1) {
            ViewControllerOne *ctl = [[ViewControllerOne alloc] init];
            [self.navigationController pushViewController:ctl animated:YES];
        }
    }

    简单说一下,就是在一个界面上有一个UITextField和一个按钮,按钮的点击事件就是弹出UIAlertView。下面说一下模拟现象的步骤。

  1. 首先在UITextField输入一下文字
  2. 输入完成后我们点击按钮,弹出UIAlertView这个时候我们监听到的键盘事件在控制台打印如下
  3. 当我们点击UIAlertView的确定按钮的时候,push到ViewControllerOne。其中ViewControllerOne的代码也就是键盘的监听,代码如下:

    #import "ViewControllerOne.h"
    
    @interface ViewControllerOne ()
    
    @end
    
    @implementation ViewControllerOne
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [self obserKeyboard];
    }
    #pragma mark - 键盘的监控
    - (void)obserKeyboard {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardDidHideNotification object:nil];
    }
    #pragma mark 键盘动作
    - (void)keyboardWillShow:(NSNotification *)notif {
        NSLog(@"键盘将要弹出-keyboardWillShow");
    }
    - (void)keyboardShow:(NSNotification *)notif {
        NSLog(@"键盘已经弹出-keyboardShow");
    }
    - (void)keyboardWillHide:(NSNotification *)notif {
        NSLog(@"键盘将要隐藏-keyboardWillHide");
    }
    - (void)keyboardHide:(NSNotification *)notif {
        NSLog(@"键盘已经隐藏-keyboardWillHide");
    
    }

    当我们点击push进去的时候,我们再看控制台的打印信息。

  4. 在ViewControllerOne里面也监测到了键盘的事件。

这个会在真机上造成当push进入下一个界面的时候,屏幕上会出现一条黑线从右划到左边。其实是键盘弹起又立马收回去的现象造成的,当然解决办法我目前是用的UIAlertViewController来代替。还说一点,其实为了解决这个问题,我当初还在点击确定按钮的时候用[self.view endEditing:YES];来防止键盘弹出,但是实际是没有卵用,键盘还是会弹出来,后来我全部换成了UIAlertViewController。

时间: 2024-07-31 04:19:24

关于UIAlertView键盘弹起的问题的相关文章

Android之监听手机软键盘弹起与关闭

背景: 在很多App开发过程中需要在Activity中监听Android设备的软键盘弹起与关闭,但是Android似乎没有提供相关的的监听API给我们来调用,本文提供了一个可行的办法来监听软键盘的弹起与关闭. 预备知识: 在manifest文件中可以设置Activity的android:windowSoftInputMode属性,这个属性值常见的设置如下: android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 那么这里值的含

全局异步和主线程异步区别、改变PlaceHolder颜色、解决键盘弹起挡住文本框问题

1.全局异步执行耗时任务 dispatch_async(dispatch_get_global_queue(0, 0), ^{ }); 2.主线程异步刷新UI dispatch_async(dispatch_get_main_queue(), ^{ }); 3.改变PlaceHolder的颜色 [username_text setValue:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] forKeyPath:@"_placeholderLab

ios 键盘弹起

#pragma mark 键盘弹起操作 - (void)keyboardWillShow:(NSNotification *)notification{    NSDictionary *info = notification.userInfo;    kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;    [UIView beginAnimations:@"kbmove" co

键盘弹起收起时不遮挡处理

view初始化时增加通知: {code} //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //增加监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] addObserver

Android防止键盘弹起

代码贴在这,希望能帮助大家. //初始状态防止键盘弹起 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

软键盘弹起,导致底部被顶上去

计算出可视界面的高度,当软键盘弹起时让底部元素隐藏掉, 当键盘收起时再让它显示,实在没办法时这种方法也不失为一种方法 var h=document.documentElement.clientHeight; $(window).resize(function(){ let height=document.documentElement.clientHeight; if(h==height){ $('.bottom').css('display','block'); }else{ $('.bott

手机键盘弹起 背景图问题

键盘弹起后,背景图会被顶上去,解决方法 <div clss="login" :style="{ height: bodyHeight + 'px' }"> mounted () { this.bodyHeight=document.documentElement.clientHeight } 原文地址:https://www.cnblogs.com/kimm/p/10722949.html

android全屏下的输入框未跟随软键盘弹起问题

最近开发中遇到,全屏模式下输入框在底部不会跟随软键盘弹起.于是网上搜索了解决的方案.大致找到了两种方案. 第一种 定义好此类 public class SoftKeyBoardListener { private View rootView;//activity的根视图 int rootViewVisibleHeight;//纪录根视图的显示高度 private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener; public S

mui防止软键盘弹起方法

解决前 解决后 <!--防止软键盘调起时,底部内容随之挤上来--> <script type="text/javascript"> //获取原始窗口的高度 var originalHeight = document.documentElement.clientHeight || document.body.clientHeight; window.onresize = function() { //软键盘弹起与隐藏 都会引起窗口的高度发生变化 var resiz