zoukankan      html  css  js  c++  java
  • 关于输入框被键盘覆盖及收回键盘的问题

    -----------ViewController.m中的内容------------

    #import "ViewController.h"

    #import "ScreenView.h"

    @interface ViewController ()<UITextFieldDelegate>

    {

        ScreenView *_screenV;//覆盖全屏

        UIView *secView;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        [self textFieldOnView:self.view];//添加子视图

        [self coverBtnOnView:self.view];//添加按钮

    }

    - (void)textFieldOnView:(UIView *)currentV{

        UITextField *tF1 = [[UITextField alloc]init];

        tF1.frame = CGRectMake(30, 100, 100, 40);

        tF1.delegate = self;

    #pragma ------设置tF1.tag = 1;

        tF1.tag = 1;

        tF1.borderStyle = UITextBorderStyleRoundedRect;

        [currentV addSubview: tF1];

        

        UITextField *tF2 = [[UITextField alloc]init];

        tF2.frame = CGRectMake(30, 500, 100, 40);

        tF2.delegate = self;

        tF2.borderStyle = UITextBorderStyleRoundedRect;

        [currentV addSubview: tF2];

        

        secView = [[UIView alloc]initWithFrame:CGRectMake(150, 200, 200, 300)];

        secView.backgroundColor = [UIColor yellowColor];

        [currentV addSubview:secView];

        

        UITextField *textF1 = [[UITextField alloc]init];

        textF1.frame = CGRectMake(30, 100, 100, 40);

        textF1.delegate = self;

        textF1.borderStyle = UITextBorderStyleRoundedRect;

        [secView addSubview: textF1];

        

        UITextField *textF2 = [[UITextField alloc]init];

        textF2.frame = CGRectMake(30, 250, 100, 40);

        textF2.delegate = self;

        textF2.borderStyle = UITextBorderStyleRoundedRect;

        [secView addSubview: textF2];

    }

    - (void)coverBtnOnView:(UIView *)currentV{

        UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeSystem];

        coverBtn.backgroundColor = [UIColor cyanColor];

        if (!_screenV) {

            [coverBtn setTitle:@"覆盖全屏" forState:UIControlStateNormal];

            [coverBtn addTarget:self action:@selector(coverScreen) forControlEvents:UIControlEventTouchUpInside];

        }else{

            [coverBtn setTitle:@"返回" forState:UIControlStateNormal];

            [coverBtn addTarget:self action:@selector(removeCoverScreen) forControlEvents:UIControlEventTouchUpInside];

        }

        

        UIView *v = [self.view viewWithTag:1];

        coverBtn.frame = v.frame;

        

        CGRect temp = coverBtn.frame;

        temp.origin.x += temp.size.width;

        coverBtn.frame = temp;

        

        [currentV addSubview:coverBtn];

    }

    - (void)removeCoverScreen{

        [_screenV removeFromSuperview];

    }

    - (void)coverScreen{

        _screenV = [[ScreenView alloc]initWithFrame:[UIScreen mainScreen].bounds];

        

    #pragma -----视图本身透明度

        _screenV.backgroundColor = [[UIColor brownColor]colorWithAlphaComponent:0.5];

    #pragma -----视图及其子视图透明度

        //    screenV.alpha = 0.7;

        

    #pragma -----创建view覆盖全屏,需要新建如ScreenView类,重写touchesBegan方法

        [[UIApplication sharedApplication].keyWindow addSubview:_screenV];

    #pragma -----如果self.view是全屏大小,下句可行,_screenV直接用UIView初始化,并能响应触摸事件

    //    [self.view addSubview:_screenV];

        

        [self textFieldOnView:_screenV];//添加textField

        [self coverBtnOnView:_screenV];

    }

    //开始编辑输入框的时候,软键盘出现,执行此事件

    -(void)textFieldDidBeginEditing:(UITextField *)textField

    {

        UIView *tempView = self.view;

        if (_screenV) {

            tempView = _screenV;

        }

        

        UIWindow *window = [[[UIApplication sharedApplication] delegate]window];

        CGRect rect = [textField convertRect:textField.bounds toView:window];//将textField相对于其父视图的坐标转换成相对于window的坐标

        

        NSInteger y = rect.origin.y + textField.frame.size.height;

        NSInteger offset = (y - (tempView.frame.size.height - 256.0));//键盘高度216 //如何获取键盘高度?

        

        

        NSTimeInterval animationDuration = 0.30f;

        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];

        [UIView setAnimationDuration:animationDuration];

        

        //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示

        if(offset > 0)

            tempView.frame = CGRectMake(0.0f, -offset, tempView.frame.size.width, tempView.frame.size.height);

        

        [UIView commitAnimations];

    }

    ////当用户按下return键或者按回车键,keyboard消失

    //-(BOOL)textFieldShouldReturn:(UITextField *)textField

    //{

    //    [textField resignFirstResponder];

    //    return YES;

    //}

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        [super touchesBegan:touches withEvent:event];

        [self.view endEditing:YES];

    }

    //输入框编辑完成以后,将视图恢复到原始状态

    -(void)textFieldDidEndEditing:(UITextField *)textField

    {

        UIView *tempView = self.view;

        if (_screenV) {

            tempView = _screenV;

        }

        tempView.frame =CGRectMake(0, 0, tempView.frame.size.width, tempView.frame.size.height);

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    -----------------ScreenView.m中的内容-----------------

    #import "ScreenView.h"

    @implementation ScreenView

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        [super touchesBegan:touches withEvent:event];

        [self endEditing:YES];

    }

    @end

    有疏漏之处敬请指正。

    END

  • 相关阅读:
    博客
    参考博客
    KMP
    串匹配
    简单数论
    B
    各种常用函数的模板以及自己的测试数据
    header
    memcached的图形界面监控
    缓存策略
  • 原文地址:https://www.cnblogs.com/aitdsr/p/4929986.html
Copyright © 2011-2022 走看看