• IOS 关闭键盘的几种方式


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

    这是一种很直接的方式, 当不再需要虚拟键盘的时候, 只要点击虚拟键盘和编辑区域以外的地方, 就可以将键盘收起, 下面的code是在UIView绑定触摸关闭键盘的方法.

    1 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    2     if (![myTextView isExclusiveTouch]) {  
    3         [myTextView resignFirstResponder];  
    4     }  
    5 }

    使用这种方式请务必记得, 操作对象的Custom Class一定是要UIView才可以.

     

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

    如果你的touchesEnded: withEvent 方法中已经有了很多的代码, 则使用方法1 将会使得代码变得结构混乱, 所以说可以考虑使用UIControl 的 TouchUpInside来收起键盘, 将以下的代码与UIControl 的TouchUpInside 链接起来即可

     

    1 - (IBAction)dismissKeyboard: (id) sender
    2 {
    3     [myTextView resignFirstResponder];
    4 }

     

    使用这种方式请务必记得, 操作对象的Custom Class一定是要UIControl才可以. 如果想在UIView上使用这种方法, 则可以直接在IB中将 Custom Class修改为UIControl即可.

     

    3. 使用自定义收起键盘的按钮

    如果当前屏幕没有手指可以点击的地方供点击来收起键盘, 自己可以制作一个按钮来收起目前的虚拟键盘, 由于按钮需要依附于虚拟键盘的状态来决定自己的现实状态, 因此必须借用 NSNotificationCenter 来帮助我们判断键盘目前的状态.

    首先在viewDidLoad: 中, 向NSNotificationCenter 进行注册, 告诉 NSNotificationCenter 我们的doneButtonShow: 方法

    1 - (void)viewDidLoad 
    2 {  
    3    [super viewDidLoad];
    4    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (doneButtonShow:) name: UIKeyboardDidShowNotification object:nil]; 
    5 }

    现在, 当虚拟键盘出现的时候, 就会自动调用doneButtonShow:方法, 接下来只需要在doneButtonShow:方法中定义按钮出现的方法即可

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

    最后是真正起作用的隐藏键盘的方法, 顺便将添加的隐藏键盘按钮也隐藏.

    1 -(void) hideKeyboard {
    2     [self.doneButton removeFromSuperview];
    3     [myTextView resignFirstResponder];5 }

     

    严重参考: http://www.gowhich.com/blog/307#2

     

  • 相关阅读:
    GlusterFS-分布式存储集群部署
    keepalived+HAproxy集群部署
    LB-HAproxy负载均衡部署
    Pacemaker高可用环境实践
    Nginx-负载均衡部署
    LB-LVS常见模式NAT/DR部署
    HTTPS原理、应用
    LDAP-autofs挂载用户验证
    GPG-非对称加密
    大数据入门学习(Linux)
  • 原文地址:https://www.cnblogs.com/tomios/p/3755595.html
Copyright © 2020-2023  润新知