【问题标题】:make iOS page scrollable when keyboard is enabled启用键盘时使 iOS 页面可滚动
【发布时间】:2014-12-11 10:42:40
【问题描述】:

我有一个用于注册的 iOS 页面,如果启用了键盘,我希望它可以滚动,因为目前我无法滚动到页面末尾的注册按钮,并且键盘隐藏了按钮。

有什么聪明的办法吗?

【问题讨论】:

标签: ios xcode swift scroll keyboard


【解决方案1】:

为了解决你的问题,有很多解决方案。从使用UIScrolView 到更改框架或约束。

如果您想使用UIScrolView,则应将注册表单UIView 插入UIScrolView 并设置内容大小。

代码狙击手如何处理键盘和滚动视图。

首先您应该知道键盘何时显示和隐藏。为此使用通知:

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

}

之后使用通知方法(keyboardWasShownkeyboardWillBeHidden)更改contentInsets

contentInsets 更改示例:

- (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;
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

最终解决方案取决于您的选择,您可以更改框架或约束以及UIScrolView 参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多