zoukankan      html  css  js  c++  java
  • iOS关闭键盘的两种简单方法

    方法一:

        //1, 关闭键盘

        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

     

     

    方法二:

       //2, 关闭键盘

     

     

        [[self findFirstResponderBeneathView:self] resignFirstResponder];

     

     

    - (UIView*)findFirstResponderBeneathView:(UIView*)view 

    {

        // Search recursively for first responder

        for ( UIView *childView in view.subviews ) {

            if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] ) 

                return childView;

            UIView *result = [self findFirstResponderBeneathView:childView];

            if ( result ) 

                return result;

        }

        return nil;

    }

     

     

    关闭键盘 退出键盘 的5种方式

     

    1、点击编辑区以外的地方(UIView)


    2、点击编辑区域以外的地方(UIControl)


    3、使用制作收起键盘的按钮


    4、使用判断输入字元


    5、关于键盘遮蔽的问题


    1,点击编辑区以外的地方(UIView)



    这是一种很直觉的方法,当不再需要使用虚拟键盘时,只要点击虚拟键盘和编辑区域外的地方,就可以将键盘收起,下面程式码是在 UIView 中内建的触碰事件方法函式,您可以参考 Touch Panel / 触碰萤幕 / 压力感应器的基本使用方式一文,找到更多关于触碰事件的方法函式。


    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
        if (![myTextView isExclusiveTouch]) {  
            [myTextView resignFirstResponder];  
        }  
    }
    如果要使用此方式请务必记得,你操作画面的 Custom Class 一定要是 UIView 才行。

    画面的 Custom Class 为 UIView

    2. 点击编辑区域以外的地方(UIControl)

     

    收起虚拟键盘的方式与前一种相同,但是如果你的触碰事件里已经且写满了程式码,那么就可以考虑使用,UIControl 的 Touch Up Inside 事件来收起键盘,方法是将以下程式码与 UIControl 的 Touch Up Inside 事件连结即可。


    - (IBAction)dismissKeyboard:(id)sender {  
        [myTextView resignFirstResponder];  
    }
     如果要使用此方式请务必记得,你操作画面的 Custom Class 一定要是 UIControl 才行。
     画面的 Custom Class 为 UIControl

     将收起键盘的方法与 UIControl 事件连结

     3. 使用制作收起键盘的按钮

     

    当没有编辑区域以外的地方可供点击来收起键盘,自己制作一个按钮来收起目前的虚拟键盘,也是一个不错的方法,由于按钮必须在虚拟键盘出现才能显示于画面上,因此必须借用 NSNotificationCenter 来帮助我们判断目前键盘的状态。
    首先在 viewDidLoad: 事件中,向 NSNotificationCenter 进行註册,告诉 NSNotificationCenter 我们的 doneButtonshow: 方法函式。

    - (void)viewDidLoad {  
       [super viewDidLoad];  
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (doneButtonshow:) name: UIKeyboardDidShowNotification object:nil];  
    }
    现在每当虚拟键盘出现时,就会自动呼叫我们自定义的 doneButtonshow: 方法函式,接下来只要在该方法函式里定义按钮出现的方法即可。



    -(void) doneButtonshow: (NSNotification *)notification {  
       doneButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];  
       doneButton.frame = CGRectMake(0, 228, 70, 35);  
       [doneButton setTitle:@"完成编辑" forState: UIControlStateNormal];  
       [doneButton addTarget: self action:@selector(hideKeyboard) forControlEvents: UIControlEventTouchUpInside];  
       
       [self.view addSubview:doneButton];  
    }


    最后是实作按钮按下去时的 hideKeyboard: 方法函式,务必记得要在函式中移除该按钮。


    -(void) hideKeyboard {  
       [doneButton removeFromSuperview];  
       [myTextView resignFirstResponder];  

    }

    4. 使用判断输入字元



    如果要使用输入特定字元(例如 return 换行字元)来收起键盘,必须先在类别内的 @interface 区段採用  协定,您可以参考 Protocol 协定的使用方式一文,获得更多关于协定的资讯。
    在采用  协定之后,接着实作出协定内的 textView:shouldChangeTextInRange:replacementText:方法函式,此方法函式会在字元输入时触发,而回传的 BOOL 值则代表该字元是否要作用,下列程式码就是在此方法函式中,使用判断输入字元的方式来收起虚拟键盘(判断字元为 return 换行字元)。


    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {  
        if ([text isEqualToString:@" "]) {  
            [myTextView resignFirstResponder];  
            return NO;  
        }  
        return YES;  
    }
    最后别忘记在 viewDidLoad: 事件中,将 UITextView 的代理物件指向自己,这样程式才能正确找到实作  协定方法函式的类别。


    - (void)viewDidLoad  {  
        [super viewDidLoad];  
        myTextView.delegate = self;  

    }

    5. 关于键盘遮蔽的问题

     

    如果您在实作上有遭遇到键盘遮蔽编辑区域的问题,可以参考使用 Animation 解决小键盘挡住 UITextField 的问题一文,透过 Core Graphic 的 Animation 功能,在键盘出现时同时移动编辑区域来解决遮蔽的问题。

     

    方法:

    1、手势(触背景)关闭键盘

    -(void)tapBackground //在ViewDidLoad中调用
    {
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce)];//定义一个手势
        [tap setNumberOfTouchesRequired:1];//触击次数这里设为1
        [self.view addGestureRecognizer:tap];//添加手势到View中
    }

    -(void)tapOnce//手势方法
    {
        [self.textFieldName resignFirstResponder];
        [self.textFieldEmail resignFirstResponder];
    }

    2、触摸背景关闭键盘(非手势)

    更改view所指向的对象类,将它的底层类由UIView更改为UIControl。此时,能够触发操作方法。

    -(IBAction)backgroundTap:(id)sender{

    [nameField resignFirstResponder];

    [numberField resignFirstResponder];

    }

    3、Return键退出键盘,连线Did End On Exit 到这个行为。

    - (IBAction)exitKeyboard:(id)sender {
        [sender resignFirstResponder];
    }

    4、如果第一响应者存在于self.view里面,就可以退出键盘

    [self.view endEditing:YES];

     

     

     

  • 相关阅读:
    HTML5开发手机应用--viewport的作用
    media query(媒体查询)和media type(媒体类型)
    html px em pt长度单位(像素 相对长度 点)知识篇
    Markdown——入门指南
    什么是 BOM头以及如何去掉
    设计网页,常见的宽度是多少像素?或者说应该选择多大的分辨率才更美观,更能提高网站的用户驻足时间!
    CSS中元素水平居中显示的方法
    CSS hack技巧大全
    主流浏览器兼容性问题---专属CSS hack
    兼容浏览器的getElementsByClassName方法
  • 原文地址:https://www.cnblogs.com/lvyinbentengzhe/p/4160189.html
Copyright © 2011-2022 走看看