当用户点击各个对象看起来无缝时,我想尽一切办法使整个视图移到适当的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起作用,我在所有项目中都使用它。您是否尝试过:
将UIScrollView添加到视图控制器的xib中
将滚动视图的类设置为TPKeyboardAvoidingScrollView(仍然
在Xib中,通过身份检查器)
将所有控件都放在该滚动视图中?
我也找到了这个解决方案:Keyboard Manager
下载演示项目
只需将KeyboardManager和SegmenedNextPrevious类拖放到您的项目中
在您的appDelegate中,只编写一行代码:
[KeyBoardManager installKeyboardManager];
祝好运!