
时间: 2019-04-12

本文向您介绍iOS防键盘阻塞的示例ios 解决键盘遮挡问题,主要包括iOS防键盘阻塞的使用案例,应用技巧,基本知识要点和注意事项的汇总,具有一定的参考价值,需要的朋友可以引用它.

当我们在UITextField中输入数据时,我们通常会弹出键盘遮挡界面. 解决方案是: 在弹出键盘时将整个UIVIew向上移动ios 解决键盘遮挡问题,并在键盘消失时恢复UIVIew.

示例代码如下:

@interface ViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField* tf;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 600, 100, 20)];
self.tf.delegate = self;
self.tf.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.tf];
}
#pragma mark life Circle
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//后台切换到前台通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground)name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[self.view endEditing:YES];
}
- (void)applicationWillEnterForeground{
[self.view endEditing:YES];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
//第一个cell不往上弹输入框的位置
// if(indexPath.row!=0){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
// }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
}
#pragma mark 键盘操作
- (void)keyboardWillChange:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
CGFloat duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
CGRect keyFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//这个64是我减去的navigationbar加上状态栏20的高度,可以看自己的实际情况决定是否减去;
CGFloat moveY = keyFrame.origin.y -self.tf.frame.origin.y-self.tf.frame.size.height;
NSLog(@"%f",moveY);
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, moveY);
}];
}
- (void)keyboardWillHide:(NSNotification *)nsnotification
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
[UIView animateWithDuration:0.2 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, 0);
}];
}
@end
上面的iOS反键盘阻止示例是我与您共享的所有内容. 我希望为您提供参考,也希望您支持该脚本主页.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-239527-1.html
美欧联盟与中俄暗盟角力中东