如果出现键盘,我想使视图自动向上移动。已经使用苹果的代码here,它运作良好。自动移动UIScrollView区域的可见性
这就是我如何管理我的对象,所以我创建了UIScrollView
,涵盖了UIView
。这UIView
由UITextField
和UIButton
组成。
这是我如何调整我的看法键盘出现时。
#pragma mark - Keyboard Handling
// Call this method somewhere in your view controller setup code.
- (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;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollView.contentInset = contentInsets;
_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, _mainView.frame.origin)) {
[self.scrollView scrollRectToVisible:_mainView.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
}
但我认为有一点让这个奇怪。当键盘出现时,它会滚动并且我的UITextField
变得可见。但我认为这太紧张了。
在我看来,这将是更好的,如果我的UITextField
移动了一点点。我的问题是,我怎样才能设置其滚动可见性?它看起来像一些变量应该有一些不断被添加在这里
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _mainView.frame.origin)) {
[self.scrollView scrollRectToVisible:_mainView.frame animated:YES];
}
谢谢你这么多,一个小提示,将不胜感激。