关于输入框被键盘覆盖及收回键盘的问题

摘要:
-----------ViewController中的内容。m--------------#import“ViewController.h”#import”ScreenView.h“@interfaceViewController()<UITextFieldDelegate>{ScreenView*_screenV;//覆盖全屏UIView*secView;}@end@implem

-----------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

免责声明:文章转载自《关于输入框被键盘覆盖及收回键盘的问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux源码Kconfig文件语法分析TSQL笔记7:临时表和表变量下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

UITextField垂直居中对齐

用xib生成的UITextField文字默认是水平左对齐,垂直居中对齐的,但是用代码生成的UITextField确是默认是水平左对齐,垂直顶对齐。到UITextField的头文件看了一下,发现只有设置水平对齐的属性,却没有垂直对齐属性。因为xib里都可以设垂直对齐属性,所以应有的,于是再到其父类中找,终于在UIControl.h中找到了,下面两个属性就是分...

UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等,iosuitextfield

(1)可以根据需要设置文本框的样式(包括形状、边框颜色、背景等)。 (2)可以根据需要设置文字显示样式(包括输入密码时的密文显示、文字横向居中、纵向居中上下、输入的文字是否首席木大写、文字超过后是否缩小还是向右滚动等)。 (3)可以根据需要设置各种不同的键盘样式(只有数字、只有字母等等)。 (4)还有inputView可以弹出一个视图,用于取代弹出键盘,暂...

UITextView 和 UITextField限制字符数和表情符号

UITextField限制字符数 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ int pMaxLength = 12; NSInteg...

ios 含有textfield的viewcontroller随键盘弹起而改变位置

首先实现 设置代理,self.textfield.delegate = self; 具体实现代码: -(void)textFieldDidBeginEditing:(UITextField *)textField{ NSTimeInterval animationDuration = 0.30f; [UIView beginAnimatio...

iOS学习——输入验证码界面封装

  在很多App中都有输入验证码的功能需求,最近项目需要也有这个功能。做完之后简单整理了一下,将实现的基本思路做下记录。实现后的效果大致如下图所示,当四位签到码全部输入时,提交按钮是可以提交的,否则提交按钮失效,不允许提交。                      1 整体布局   上图整个界面的布局很简单,就不多说了,重点就是中间这一块的验证码输入功能...

iOS UIAlertView添加输入框

这玩意有时不用就忘,还是记录一下吧 添加: UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"新建文件夹" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [a...