2016-04-06 17 views
0

如果出现键盘,我想使视图自动向上移动。已经使用苹果的代码here,它运作良好。自动移动UIScrollView区域的可见性

这就是我如何管理我的对象,所以我创建了UIScrollView,涵盖了UIView。这UIViewUITextFieldUIButton组成。

Document Outline

这是我如何调整我的看法键盘出现时。

#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变得可见。但我认为这太紧张了。

Result

在我看来,这将是更好的,如果我的UITextField移动了一点点。我的问题是,我怎样才能设置其滚动可见性?它看起来像一些变量应该有一些不断被添加在这里

CGRect aRect = self.view.frame; 
aRect.size.height -= kbSize.height; 
if (!CGRectContainsPoint(aRect, _mainView.frame.origin)) { 
    [self.scrollView scrollRectToVisible:_mainView.frame animated:YES]; 
} 

注意: 结果,我想 Expectation

谢谢你这么多,一个小提示,将不胜感激。

回答

0

解决

我解决了这个由管理增加一些数量插页内容。 在keyboardWasShown:中,我通过我的文本框和按钮的高度添加了它的内容。假设它总共是100,所以就是这样。

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+100, 0.0); 

非常感谢。

2

最简单的解决方案是在键盘打开时移动视图(或滚动视图)。

- (void)keyboardWillShow:(NSNotification*)notification{ 
    [self.view setFrame:CGRectMake(0,-100, self.view.frame.size.width, self.view.frame.size.height)]; // where 100 is the offset 
    [self.view setNeedsDisplay]; 

} 

- (void)keyBoardWillHide:(NSNotification*)notification{ 
    [self.view setFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)]; 
    [self.view setNeedsDisplay]; 
}