键盘到位后移动UIScrollView [英] Move UIScrollView when keyboard comes into place
问题描述
问题已经解决。我没有在 UIBuilder
中正确链接我的代理。代码很好!
The problem has been solved. I did not have my delegates linked properly in UIBuilder
. Code is good!
我正在尝试在键盘出现时调整scrollview的大小。我去了开发人员文档并找到了这些信息。
I am trying to resize a scrollview when the keyboard appears. I went to the developer docs and found this information.
左侧管理键盘 。
在文档中,它显示了一些代码来检测键盘的大小,然后调整 UIScrollView
。我在函数的代码中放置了一个
所以我看到该函数实际被调用,但当我尝试 NSLog
消息 - (void)keyboardWasShown:(NSNotification *)aNotification NSLog
kbSize
。高度时,它总是被重视在0。
In the documentation it shows a bit of code to detect the size of the keyboard and then to resize a UIScrollView
. I have placed an NSLog
message in the code for function - (void)keyboardWasShown:(NSNotification*)aNotification
so I see that the function is actually being called, but when I try to to NSLog
the kbSize
.height it is always valued at 0.
为什么苹果为此目的提供的代码不起作用?
Why does the code that apple provide for this purpose not work?
- (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 application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
推荐答案
你可能想尝试强烈推荐的TPKeyboardAvoidingScrollView,可从以下网址获得: https://github.com/michaeltyson/TPKeyboardAvoiding
You may want to try the highly recommended "TPKeyboardAvoidingScrollView", available from: https://github.com/michaeltyson/TPKeyboardAvoiding
像魅力一样......
Works like a charm...
这篇关于键盘到位后移动UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!