• TextSendKeyboardViewController文字发送键盘


    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
  • 相关阅读:
    jquery ajax你会了吗?
    JS写Cookie
    [活动通知]Nanjing GDG 2013年4月活动
    UITableView刷新数据reLoadData
    FMS4.0 客户端访问服务器脚本文件 main.asc
    关于IOS6屏幕旋转
    Windows平台下Makefile学习笔记(二)
    编译MapWindowGis源码出现的重定义的问题及解决办法
    你的 mixin 兼容 ECMAScript 5 吗?
    有点坑爹的gdal库
  • 原文地址:https://www.cnblogs.com/MartinLi841538513/p/4224832.html
Copyright © 2020-2023  润新知