我正在使用Apple提出的揭示UITextfield的方法,该方法在被选中后会被键盘隐藏(见清单5-1):https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1
这对我不起作用,因为每次我设置conentInset时,我的scrollView会自动向上滚动几个像素就可以了。即使我删除了scrollRectToVisible调用,我的scrollView仍会向上移动。如果我在viewDidLoad中设置contentInset,那么一切都可以作为例外。
注意:我目前没有使用autolayout。如果我在任何事情都没有发生自动布局。
有什么建议吗?
答案 0 :(得分:1)
设置contentInset
,setContentOffset
和scrollIndicatorInsets
示例:强>
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Method is used to handle the keyboard visibility.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
- (void)keyboardWasShown:(NSNotification *)notification
{
@try
{
// Step 1: Get the size of the keyboard.
CGSize keyboardSizePotriat = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGSize keyboardSize = {keyboardSizePotriat.height,keyboardSizePotriat.width};
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.theScrollView.contentInset = contentInsets;
self.theScrollView.scrollIndicatorInsets = contentInsets;
// Modify the scrollPoint as per your screen.
CGPoint scrollPoint = CGPointMake(0.0, self.activeTextField.frame.origin.y - (keyboardSize.height - 45));
[self.theScrollView setContentOffset:scrollPoint animated:YES];
}
@catch (NSException *exception)
{
NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}
查看以下链接了解更多详情