[英]Scrolling view when keyboard appears: Library or DIY?
I've had it up to my neck trying to make the whole view moving to the appropriate UITextField
when a user taps on a respective object look seamless. 当用户点击相应的对象看起来无缝时,我想尽一切办法使整个视图移到适当的
UITextField
。 I know that I'm not the only one that absolutely hates doing this too. 我知道我并不是唯一一个绝对不喜欢这样做的人。
What is the best approach to making this work as beautifully as possible with the least amount of work possible? 用最少的工作量使工作尽可能精美的最佳方法是什么?
I've tried out TPKeyboardAvoiding , and it totally sucked. 我已经尝试了TPKeyboardAvoiding ,它完全烂透了 。
Right now, I've got this code going, but it sucks as well in it's own special way: 现在,我已经编写了这段代码,但是它以其自己的特殊方式也很烂:
- (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;
}
For me, it works TPKeyboardAvoiding , I use it in all my projects. 对我来说,它可以工作TPKeyboardAvoiding ,我在所有项目中都使用它。 Have you tried to:
您是否尝试过:
I also find this solution : Keyboard Manager 我也找到了这个解决方案: 键盘管理器
In your appDelegate write only one line of code: 在您的appDelegate中,只编写一行代码:
[KeyBoardManager installKeyboardManager]; [KeyBoardManager installKeyboardManager];
Good luck! 祝好运!
Did you know that if your view controller derives from UITableViewController, scrolling the table view is automatic when the keyboard displays when a text field or text view gets focus? 您是否知道,如果您的视图控制器是从UITableViewController派生的,那么当文本字段或文本视图获得焦点时,当键盘显示时,表视图的滚动是自动的? You don't have to do anything.
您不必做任何事情。 That being said, it may not work well for your app to refactor the UI so that it uses a table view with static cells instead of whatever you're doing now, though.
话虽这么说,但是对于您的应用来说,重构UI可能不太好,因此它使用带有静态单元格的表视图来代替您现在正在做的任何事情。
If this wont work for you, you can change your scroll view's contenSize
to be the size of your visible area when the keyboard is shown and then call scrollRectToVisible:animated: on your scroll view passing it the rect of your textfield inside of your keyboardWasShown: selector. 如果这对您
contenSize
,则可以在显示键盘时将滚动视图的contenSize
更改为可见区域的大小,然后在滚动视图上调用scrollRectToVisible:animated : ,将其传递给键盘内部文本字段的rectWasShown:选择器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.