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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

[[UIapplicationsharedApplication].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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航