[英]iOS 8 Keyboard - Issue with pushing the UITextField up when keyboard was shown
[英]Scrolling UITextField into view when keyboard is shown
我正在編寫針對iOS 6.1的應用程序。 我正在嘗試遵循此Apple文檔 ,以了解如何將UITextField滾動到視圖中(如果鍵盤遮擋了它)。 問題在於,Apple記錄的用於計算滾動點的算法不能很好地工作。 我計算滾動點的算法效果稍好,但減少了70個像素。 計算滾動點的正確方法是什么?
在下面您可以從左到右看到顯示鍵盤之前的視圖,使用Apple算法計算滾動點后的視圖以及使用算法計算滾動點后的視圖。 (該網格中的每個正方形都是25像素乘25像素。)
這是我正在使用的代碼。 注意#if APPLE_WAY
塊。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.view.contentInset = contentInsets;
self.view.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
#if APPLE_WAY
CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
#else
CGFloat offset = self.activeField.frame.origin.y;
//TODO: Why is this off by 70?
offset = offset - 70;
CGPoint scrollPoint = CGPointMake(0.0, offset);
#endif
[self.view setContentOffset:scrollPoint animated:YES];
}
}
您只需要將偏移量設置為鍵盤的高度:
CGFloat offset = kbSize.height;
CGPoint scrollPoint = CGPointMake(0.0, offset);
[self.view setContentOffset:scrollPoint animated:YES];
希望這可以幫助
有幾種方法可以做到這一點。 鑒於您的情況,我將執行以下操作:
- (void)keyboardWasShown:(NSNotification *)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.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 application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0, 0);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}
然后,在您的viewDidLoad方法中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
當您擁有滾動視圖時,這很容易,因為您甚至不必滾動; 您所要做的就是調整contentInset和scrollIndicatorInsets使其脫離鍵盤,其余的自動發生:
- (void) keyboardShow: (NSNotification*) n {
self->_oldContentInset = self.scrollView.contentInset;
self->_oldIndicatorInset = self.scrollView.scrollIndicatorInsets;
self->_oldOffset = self.scrollView.contentOffset;
NSDictionary* d = [n userInfo];
CGRect r = [[d objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
r = [self.scrollView convertRect:r fromView:nil];
CGRect f = self.fr.frame;
CGFloat y =
CGRectGetMaxY(f) + r.size.height -
self.scrollView.bounds.size.height + 5;
if (r.origin.y < CGRectGetMaxY(f))
[self.scrollView setContentOffset:CGPointMake(0, y) animated:YES];
UIEdgeInsets insets;
insets = self.scrollView.contentInset;
insets.bottom = r.size.height;
self.scrollView.contentInset = insets;
insets = self.scrollView.scrollIndicatorInsets;
insets.bottom = r.size.height;
self.scrollView.scrollIndicatorInsets = insets;
}
在本書的這一部分中,我用代碼給出了一系列不同的策略:
http://www.apeth.com/iOSBook/ch23.html#_keyboard_covers_text_field
所有這些都由可在我的github站點上獲取的可下載項目支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.