ios - 带有文本框的 Storyboard ScrollView

标签 ios uiscrollview xcode-storyboard

我正在 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/

相关文章:

swift - Tables NSTabViewController

ios - Swift 中的 NSURL 数组错误?

ios - 在 UIView 上绘制径向渐变(圆形)

ios - SpriteKit - 从非常大的图像(640 x 6000)创建滚动背景

objective-c - 如何控制嵌套的 ScrollView

ios - UIScrollView 内部的 UIScrollView

ios - 上传到 AppStore 时,应用程序包装器必须以 .app 结尾

ios - 使用按钮将 childViewController 加载到 scrollview parent

ios - 获取使用 Storyboard创建的 View Controller 的属性

swift - 无法使用自定义 segue 和 Storyboard 调整 UIViewController 的大小