How to move UIScrollView to the active UITextField while the device is laying flat?
好的,所以我对这个很难过。 我正在使用键盘通知和UIScrollView在点击时滚动到当前活动文本字段。 当设备平放时,它在纵向和横向上的工作方式与预期完全相同。
在横向方向,如果设备保持在空中,它会按预期滚动,但当设备平放在桌子上并轻敲文本字段时,视图将完全滚出屏幕。
是什么导致这个?
我正在使用iOS7在iPad Mini和iPhone 5上进行测试。
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 61 62 63 64 65 66 67 68 69 70 71 | - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // Register for keyboard notifications [self registerForKeyboardNotifications]; // keep track of scrollview insets for later scollViewIndicatorInsets = self.scrollView.scrollIndicatorInsets; } - (IBAction)editingEnded:(id)sender { [sender resignFirstResponder]; } - (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } // Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) { kbSize = CGSizeMake(kbSize.height, kbSize.width); } UIEdgeInsets contentInsets = UIEdgeInsetsMake(65.0, 0.0, kbSize.height, 0.0); self.scrollView.contentInset = contentInsets; self.scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it's visible // Your app might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; } } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { // UIEdgeInsets contentInsets = UIEdgeInsetsZero; UIEdgeInsets contentInsets = UIEdgeInsetsMake(65.0, 0.0, 0.0, 0.0); self.scrollView.contentInset = contentInsets; self.scrollView.scrollIndicatorInsets = contentInsets; } - (void)textFieldDidBeginEditing:(UITextField *)textField { activeField = textField; } - (void)textFieldDidEndEditing:(UITextField *)textField { activeField = nil; } |
键盘大小的计算比它应该复杂得多。 让API为您完成工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGRect kbFrame = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue]; // Convert the keyboard frame from the window to the scrollview kbFrame = [self.scrollView convertRect:kbFrame fromView:nil]; CGSize kbSize = kbFrame.size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(65.0, 0.0, kbSize.height, 0.0); self.scrollView.contentInset = contentInsets; self.scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it's visible // Your app might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; } } |