博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TextSendKeyboardViewController文字发送键盘
阅读量:4947 次
发布时间:2019-06-11

本文共 3698 字,大约阅读时间需要 12 分钟。

github 下载demo:https://github.com/MartinLi841538513/MartinDemos (一切以demo为准)

这里引入了一个第三方库DAKeyboardControl,主要是监听键盘的变化。

////  ViewController.m//  TextSendKeyboardDemo////  Created by Gao Huang on 15-1-14.//  Copyright (c) 2015年 Martin. All rights reserved.//#import "ViewController.h"#import "DAKeyboardControl.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *label;@property(nonatomic,strong)UIToolbar *toolBar;@property(nonatomic,strong)UITextField *textField;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];        self.title = @"DAKeyboardControl";    [self setupTextSendKeyboard];    }-(void)setupTextSendKeyboard{    self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f,                                                               self.view.bounds.size.height - 40.0f,                                                               self.view.bounds.size.width,                                                               40.0f)];    self.toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;    self.toolBar.hidden=YES;    [self.view addSubview:self.toolBar];        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f,                                                                   6.0f,                                                                   self.toolBar.bounds.size.width - 20.0f - 68.0f,                                                                   30.0f)];    self.textField.borderStyle = UITextBorderStyleRoundedRect;    self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;    [self.toolBar addSubview:self.textField];        UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    sendButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;    [sendButton setTitle:@"发送" forState:UIControlStateNormal];    sendButton.frame = CGRectMake(self.toolBar.bounds.size.width - 68.0f,                                  6.0f,                                  58.0f,                                  29.0f);    [sendButton addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];    [self.toolBar addSubview:sendButton];            self.view.userInteractionEnabled = YES;    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];    [self.view addGestureRecognizer:tap];        self.view.keyboardTriggerOffset = self.toolBar.bounds.size.height;    __weak typeof(self) weakSelf = self;    [self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {        /*         Try not to call "self" inside this block (retain cycle).         But if you do, make sure to remove DAKeyboardControl         when you are done with the view controller by calling:         [self.view removeKeyboardControl];         */        CGRect toolBarFrame = weakSelf.toolBar.frame;        toolBarFrame.origin.y = keyboardFrameInView.origin.y - toolBarFrame.size.height;        weakSelf.toolBar.frame = toolBarFrame;    }];}-(void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];    [self.view removeKeyboardControl];}-(void)sendAction:(UIButton *)sender{    NSLog(@"%@",self.textField.text);    [self hideKeyboard];    self.label.text = self.textField.text;    self.textField.text = @"";}-(void)showKeyboard{    [self.textField becomeFirstResponder];    self.toolBar.hidden = NO;}-(void)hideKeyboard{    [self.textField resignFirstResponder];    self.toolBar.hidden = YES;}- (IBAction)makeComment:(id)sender {    [self showKeyboard];}@end

 

转载于:https://www.cnblogs.com/MartinLi841538513/p/4224832.html

你可能感兴趣的文章
什么是死锁,简述死锁发生的四个必要条件,如何避免与预防死锁
查看>>
静态方法是否属于线程安全
查看>>
fegin 调用源码分析
查看>>
《Java多线程编程核心技术》读后感(三)
查看>>
344. Reverse String
查看>>
pygame系列
查看>>
polyLine画折返线时绘图出头的问题
查看>>
GJM : 安装SqlServer遇到问题的解决方案 [原创]
查看>>
获取ip 笔记
查看>>
文本溢出
查看>>
环状链表问题
查看>>
Qt的QWebChannel和JS、HTML通信/交互驱动百度地图
查看>>
异步和多线程,委托异步调用,Thread,ThreadPool,Task,Parallel,CancellationTokenSource
查看>>
spring-boot(hello world)
查看>>
0712-----C++Primer听课笔记----------IO流
查看>>
前端数据库——WebSQL和IndexedDB
查看>>
SUOI #69 奔跑的Aqua
查看>>
【机器学习】模型泛化
查看>>
HTML5-入门2。
查看>>
WIN32 listview用法
查看>>