日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS 键盘遮挡输入框万能解决方案(多个输入框)

發布時間:2025/3/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 键盘遮挡输入框万能解决方案(多个输入框) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 效果圖如下:
  • 思路分析:
  • 代碼:
  • 知識點:
  • 問題:

效果圖如下:

思路分析:

當我們有很多輸入框時,有時候鍵盤彈出來會遮擋著輸入框。我們需要獲取輸入框和鍵盤相對于最外層視圖的位置來判斷是否遮擋,如果遮擋了計算出遮擋的高度,然后設置最外層視圖的frame,往上移動到大于等于遮擋遮住的高度即可。當鍵盤隱藏是在講最外層視圖的frame還原回來。

代碼:

Main.storyboard如下所示:

#import "ViewController.h" @interface ViewController () @property(nonatomic ,strong) UITextField * firstResponderTextF;//記錄將要編輯的輸入框 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //監聽鍵盤展示和隱藏的通知 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)dealloc{ //移除鍵盤通知監聽者 [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ if ([self.firstResponderTextF isFirstResponder])[self.firstResponderTextF resignFirstResponder]; } #pragma maek UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ self.firstResponderTextF = textField;//當將要開始編輯的時候,獲取當前的textField return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } #pragma mark : UIKeyboardWillShowNotification/UIKeyboardWillHideNotification - (void)keyboardWillShow:(NSNotification *)notification{ CGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self.view];//獲取相對于self.view的位置 NSDictionary *userInfo = [notification userInfo]; NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];//獲取彈出鍵盤的fame的value值 CGRect keyboardRect = [aValue CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window];//獲取鍵盤相對于self.view的frame ,傳window和傳nil是一樣的 CGFloat keyboardTop = keyboardRect.origin.y; NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//獲取鍵盤彈出動畫時間值 NSTimeInterval animationDuration = [animationDurationValue doubleValue]; if (keyboardTop < CGRectGetMaxY(rect)) {//如果鍵盤蓋住了輸入框 CGFloat gap = keyboardTop - CGRectGetMaxY(rect) - 10;//計算需要網上移動的偏移量(輸入框底部離鍵盤頂部為10的間距) __weak typeof(self)weakSelf = self; [UIView animateWithDuration:animationDuration animations:^{ weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, gap, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height); }]; } } - (void)keyboardWillHide:(NSNotification *)notification{ NSDictionary *userInfo = [notification userInfo]; NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//獲取鍵盤隱藏動畫時間值 NSTimeInterval animationDuration = [animationDurationValue doubleValue]; if (self.view.frame.origin.y < 0) {//如果有偏移,當影藏鍵盤的時候就復原 __weak typeof(self)weakSelf = self; [UIView animateWithDuration:animationDuration animations:^{ weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height); }]; } } @end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

知識點:

1.當輸入框將要編輯時會調- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField代理方法(本為用的都是UITextField),此時用一個全局變量firstResponderTextF來記錄將要編輯的輸入框。(好處就是如果很多輸入框是局部變量一個個獲取會比較麻煩,用一個全局變量來記錄就會簡單方便的多);
2. 鍵盤展示和隱藏的通知:UIKeyboardWillShowNotification,UIKeyboardWillHideNotification;可以獲得鍵盤的frame和動畫時長;通過計算鍵盤和輸入框相對于最外層是視圖的外置來判斷是否被哲哲,如果遮住則間整體視圖網上移動大于等于遮住的高度,當鍵盤隱藏的時候則還原來的位置;

對應的key:UIKeyboardFrameEndUserInfoKey鍵盤frame,UIKeyboardAnimationDurationUserInfoKey展示和影藏的動畫時長;
3.相對于摸個視圖位置的api(UIView):

- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;//獲取當前視圖的點坐標相對于view上的點坐標 - (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;//獲取view上的點坐標相對于當前視圖的點坐標 - (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;//獲取當前視圖的frame相對于view上的frame - (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;//獲取view上的frame相對于當前視圖的frame
  • 1
  • 2
  • 3
  • 4

問題:

如文中獲取將要編輯的輸入框相對于最外層視圖的fameCGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self.view];(這里其實應該算兩次:要先獲得輸入框在父視圖(紅色或藍色的view)中的位置來獲得相對父視圖的俯視圖(黃色的view)的位置;然后才能獲得輸入框相對于最外層視圖(白色的view),但文我直接用輸入框在父視圖(紅色或藍色的view)中的位置獲得到了相對于最外層view(白色的view)的位置,并且結果正確,所以有些困惑,有理解的大神煩請指點一二);
原碼:iOS 鍵盤遮擋輸入框萬能解決方案(多個輸入框)

轉載于:https://www.cnblogs.com/Free-Thinker/p/9371662.html

總結

以上是生活随笔為你收集整理的iOS 键盘遮挡输入框万能解决方案(多个输入框)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。