关闭键盘

    xiaoxiao2021-03-25  182

    关闭键盘有以下几种方式: 1、在ViewController中的view被覆盖的情况下,可以通过触摸编译区以外来关闭键盘 - (void)viewDidLoad { [super viewDidLoad]; [self.tableView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]]; } - (void)tap { [self.view endEditing:YES]; }

    2、在ViewController中的view不被覆盖的情况下,通过触摸背景关闭键盘。使用Interface builder,将view的底层类由UIView改为UIController,添加如下代码:

    - (IBAction)backgrooundTap : (id)sender { [self.nameField resignFirstResponder]; }

    再次选中storyboard,打开关联检查器,把Touch Down事件拖到View Controller图标上,选择backgroundTap操作方法。

    3、设置一个按钮,通过点击按钮实现关闭键盘

    - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (doneButtonshow:) name: UIKeyboardDidShowNotification object:nil]; } -(void) doneButtonshow: (NSNotification *)notification { self.doneButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; self.doneButton.frame = CGRectMake(350, 430, 70, 35); [self.doneButton setTitle:@"完成编辑" forState: UIControlStateNormal]; [self.doneButton addTarget: self action:@selector(hideKeyboard) forControlEvents: UIControlEventTouchUpInside]; [self.view addSubview:self.doneButton]; } -(void) hideKeyboard { [self.doneButton removeFromSuperview]; [self.messageCell.textView resignFirstResponder]; }
    转载请注明原文地址: https://ju.6miu.com/read-8702.html

    最新回复(0)