UITextView和UITextField退出键盘的方式

  1. 1、(点击Return关闭键盘)textField中用xib我们右键点击有个

    方法Did end on exit 脱线到控制器。然后生成IBAction方法,

    就是我们点击键盘的右下角的return所触发的事件。这里我们可以

    - (IBAction)finish:(id)sender {

        [sender resignFirstResponder];

    }

    退出键盘

    2、(触碰背景关闭键盘)点击xib应用界面的背景界面,然后按键盘上

    command+shift+3快捷键,就可以打开Xcode的身份检查器面板,

    在该面板中将该控件的实现类改为“UIControl”<本来应该是UIView

    UIControl继承于UIView,因此具有UIView的所有外观和行为>

    然后这个背景脱线到控制器(这里拖线用的方法是Touch Down),

    就是随便点击该背景的任意一处就会触发的一个事件。我们可以点击

    任意一处做退出键盘的操作。


    上面是textField退出键盘的方式

    UITextView继承了UIScrollVeiw:UIView控件  因此它默认带有滚动条

    UITextViewUITextField退出键盘的方式可能不太一样,因为UITextVeiwreturn键通常代表换行,所以这里介绍不同的方式退出键盘,而且该控件通常会占满整个屏幕,所以如果要关闭虚拟键盘,大致有以下两种方式:

    1、如果程序自带导航条,可通过导航按钮来关闭虚拟键盘

    2、通过自定义虚拟键潘附件来关闭虚拟键盘。

  2. 这里要注意学习导航条的设置

    方法1:如果程序自带导航条,可通过导航按钮来关闭虚拟键盘
    #import "ViewController.h"
    @interface ViewController ()<UITextViewDelegate>
    {
        UIBarButtonItem *done;
        
    }
    @property (strong, nonatomic) IBOutlet UITextView *textView;
    @property (strong, nonatomic) UINavigationItem *navItem;
    @end
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.textView.delegate = self;
        //创建并添加导航条
        UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
        [self.view addSubview:navbar];
        //创建导航项,并设置导航项的标题
    //    方法一:只能设置导航项的标题
    //    UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@"导航条"];
    //    [navbar pushNavigationItem:navItem animated:YES];
        
    //    方法二:
        self.navItem = [[UINavigationItem alloc]initWithTitle:@"导航条"];
        //将导航项添加到到航条中
        navbar.items = [NSArray arrayWithObject:self.navItem];
        //创建一个UIBarButtonItem对象,并赋给done属性
        done = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(finishEdit)];
    }
    -(void)textViewDidBeginEditing:(UITextView *)textView
    {
        self.navItem.rightBarButtonItem = done;
    }
    -(void)textViewDidEndEditing:(UITextView *)textView
    {
        self.navItem.leftBarButtonItem = nil;
    }
    -(void)finishEdit
    {
        [self.textView resignFirstResponder];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
    这里要注意学习工具条的设置
    方法2:定义虚拟键盘附件来关闭虚拟键盘
    //  ViewController.m
    //  UITextVeiw
    //
    //  Created by xxt-imac on 16/1/6.
    //  Copyright © 2016年 xxt-imac. All rights reserved.
    //
    #import "ViewController.h"
    @interface ViewController ()<UITextViewDelegate>
    @property (strong, nonatomic) IBOutlet UITextView *textView;
    ​@property (strong, nonatomic) UINavigationItem *navItem;
    @end
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        //创建toolBar工具条
        UIToolbar *topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
        //设置工具条风格
        [topView setBarStyle:UIBarStyleDefault];
        //为工具条创建第一个“按钮”
        UIBarButtonItem *myBn = [[UIBarButtonItem alloc]initWithTitle:@"无动作" style:UIBarButtonItemStyleBordered target:self action:nil];
        //为工具条创建地二个“按钮”,该按钮就是一片刻伸缩的空白区
        UIBarButtonItem *spaceBn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        //为工具条创建第三个按钮,单击该按钮会激发editFinish方法
        UIBarButtonItem *doneBn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(editFinish)];
        //以三个按钮小黄件NSArray集合
        NSArray *buttonArray = [NSArray arrayWithObjects:myBn,spaceBn,doneBn, nil];
        //为UIToolBar设置按钮
        [topView setItems:buttonArray];
        //为textView关联的虚拟键盘设置附件
        [self.textView setInputAccessoryView:topView];
        
    }
    -(void)editFinish
    {
        [self.textView resignFirstResponder];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end


你可能感兴趣的:(ios,textview,控件,导航条,UIToolbar)