[英]Scrolling view when keyboard appears: Library or DIY?
当用户点击相应的对象看起来无缝时,我想尽一切办法使整个视图移到适当的UITextField
。 我知道我并不是唯一一个绝对不喜欢这样做的人。
用最少的工作量使工作尽可能精美的最佳方法是什么?
我已经尝试了TPKeyboardAvoiding ,它完全烂透了 。
现在,我已经编写了这段代码,但是它以其自己的特殊方式也很烂:
- (void)viewDidLoad
{
self.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin);
self.scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (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
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
对我来说,它可以工作TPKeyboardAvoiding ,我在所有项目中都使用它。 您是否尝试过:
我也找到了这个解决方案: 键盘管理器
在您的appDelegate中,只编写一行代码:
[KeyBoardManager installKeyboardManager];
祝好运!
您是否知道,如果您的视图控制器是从UITableViewController派生的,那么当文本字段或文本视图获得焦点时,当键盘显示时,表视图的滚动是自动的? 您不必做任何事情。 话虽这么说,但是对于您的应用来说,重构UI可能不太好,因此它使用带有静态单元格的表视图来代替您现在正在做的任何事情。
如果这对您contenSize
,则可以在显示键盘时将滚动视图的contenSize
更改为可见区域的大小,然后在滚动视图上调用scrollRectToVisible:animated : ,将其传递给键盘内部文本字段的rectWasShown:选择器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.