我正在 Storyboard中创建一个 View ,该 View 有一堆文本框,底部有一个按钮。当我在文本框中单击时,键盘出现。这隐藏了我的一些文本框。所有文本框都在 ScrollView 中。 Storyboard中是否有一种方法(如果没有,则在代码中)使 ScrollView 在键盘出现时在所有内容中正确滚动?
我是否必须在键盘出现时动态更改内容大小,或者是否有一种方法可以设置 ScrollView ,以便在键盘出现时自动调整大小。
最佳答案
我想我遇到了和你一样的问题,我所做的是在键盘出现或隐藏时动态更改 ScrollView 的 contenSize 属性。
我在管理 ScrollView 的 ViewController 类中订阅了键盘通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
然后在目标方法中调整大小:
- (void)keyboardWillShow:(NSNotification *)note{
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
//This is the value of the rect where the keyboard is drawn, and may come with height and width swapped
CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | curve
animations:^{
yourScrollableView.frame = ......
}
completion:nil];
当然这同样适用于键盘隐藏。
关于ios - 带有文本框的 Storyboard ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267966/