有個(gè)功能是點(diǎn)擊輸入框鍵盤(pán)彈出的時(shí)候不讓遮擋辩昆,就是鍵盤(pán)彈起時(shí)輸入框自動(dòng)上移,效果如下:
效果圖
具體實(shí)現(xiàn)代碼:
// KeyboardToMoveController.m
// JSOCTest
//
// Created by 賈倍 on 2018/4/17.
// Copyright ? 2018年 賈倍. All rights reserved.
//
#import "KeyboardToMoveController.h"
@interface KeyboardToMoveController (){
UITextField *phoneNemberText;//手機(jī)號(hào)輸入框
UITextField *verifyCodeText;//密碼輸入框
}
@end
@implementation KeyboardToMoveController
- (void)viewDidLoad {
[super viewDidLoad];
phoneNemberText = [[UITextField alloc] initWithFrame:FRAME(100, SCREEN_HEIGHT*0.7, 100, 30)];
phoneNemberText.placeholder = @"手機(jī)號(hào)";
phoneNemberText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:phoneNemberText];
verifyCodeText = [[UITextField alloc] initWithFrame:FRAME(100, phoneNemberText.mr_bottom + 20, 100, 30)];
verifyCodeText.placeholder = @"密碼";
verifyCodeText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:verifyCodeText];
//注冊(cè)觀察鍵盤(pán)的變化
//鍵盤(pán)將要顯示
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//鍵盤(pán)將要隱藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// Do any additional setup after loading the view.
}
-(void)keyboardWillShow:(NSNotification *)notification{
//獲取處于焦點(diǎn)中的view
NSArray *textFields = @[phoneNemberText, verifyCodeText];
UIView *focusView = nil;
for (UITextField *view in textFields) {
if ([view isFirstResponder]) {
focusView = view;
break;
}
}
if (focusView) {
//獲取鍵盤(pán)彈出的時(shí)間
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//獲取鍵盤(pán)上端Y坐標(biāo)
CGFloat keyboardY = [self getKeyboardY:notification.userInfo];
//獲取輸入框下端相對(duì)于window的Y坐標(biāo)
CGPoint tmp = [self getViewOriginPointToWindow:focusView];
CGFloat inputBoxY = tmp.y + focusView.frame.size.height;
//計(jì)算二者差值
CGFloat ty = keyboardY - inputBoxY;
NSLog(@"position keyboard: %f, inputbox: %f, ty: %f", keyboardY, inputBoxY, ty);
//差值小于0鹦肿,做平移變換
[UIView animateWithDuration:duration animations:^{
if (ty < 0) {
self.view.transform = CGAffineTransformMakeTranslation(0, ty);
}
}];
}
}
-(void)keyboardWillHide:(NSNotification *)notification{
//獲取鍵盤(pán)彈出的時(shí)間
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//還原
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, 0);
}];
}
//處理iOS8之后鍵盤(pán)上端的Y坐標(biāo)以及輸入框下端相對(duì)于window的Y坐標(biāo)
-(CGFloat)getKeyboardY:(NSDictionary *)userInfo{
CGFloat screenHeight;
CGFloat keyboardY = 0;
CGFloat keyboardHeight = 0;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (([[[UIDevice currentDevice] systemVersion] floatValue]<8) && UIInterfaceOrientationIsLandscape(orientation)) {
screenHeight = [[UIScreen mainScreen] bounds].size.width;
keyboardHeight = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.width;
keyboardY = screenHeight - keyboardHeight;
} else if (([[[UIDevice currentDevice] systemVersion] floatValue]<8) && UIInterfaceOrientationIsPortrait(orientation)) {
screenHeight = [[UIScreen mainScreen] bounds].size.height;
keyboardHeight = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
keyboardY = screenHeight - keyboardHeight;
} else {
keyboardY = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
}
return keyboardY;
}
//獲取輸入框下端的Y坐標(biāo)
-(CGPoint)getViewOriginPointToWindow:(UIView *)view{
CGPoint origin;
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8) {
CGPoint focusViewPoint = [view convertPoint:CGPointZero toView:nil];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft) {
origin.y = focusViewPoint.x;
origin.x = [[[UIApplication sharedApplication] delegate] window].bounds.size.height - focusViewPoint.y;
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
origin.y = [[[UIApplication sharedApplication] delegate] window].bounds.size.width - focusViewPoint.x;
origin.x = focusViewPoint.y;
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
origin.y = [[[UIApplication sharedApplication] delegate] window].bounds.size.height - focusViewPoint.y;
origin.x = [[[UIApplication sharedApplication] delegate] window].bounds.size.width - focusViewPoint.x;
} else {
origin = focusViewPoint;
}
} else {
CGRect rect = [view convertRect:view.bounds toView:[[[UIApplication sharedApplication] delegate] window]];
origin = rect.origin;
}
return origin;
}
//點(diǎn)擊空白區(qū)域讓鍵盤(pán)回收
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
for (UIView *view in self.view.subviews) {
[view resignFirstResponder];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end