自动移动UIScrollView区域的可见性

时间:2016-04-06 07:17:25

标签: ios objective-c uiview uiscrollview

如果键盘出现,我想让我的视图自动向上移动。已经使用了苹果公司的代码here,效果很好。

这就是我管理对象的方式,因此我创建了一个涵盖UIScrollView的{​​{1}}。此UIViewUIViewUITextField组成。

Document Outline

这是键盘出现时调整视图的方法。

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; } 变得可见。但我觉得它太紧了。

Result

在我看来,如果我的UITextField向上移动一点点会更好。我的问题是,如何设置其滚动可见性?看起来某些变量应该添加一些常量

UITextField

注意: 我想要的结果 Expectation

非常感谢,一点点的提示将不胜感激。

2 个答案:

答案 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];
}

答案 1 :(得分:0)

<强>解决

我通过添加一些数字来管理其内容插入来解决这个问题。 在keyboardWasShown:中,我添加了内容插入文本字段和按钮的高度。假设它总共100个,所以就是这样。

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

非常感谢你。