启用键盘时可以使iOS页面可滚动

前端之家收集整理的这篇文章主要介绍了启用键盘时可以使iOS页面可滚动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个用于注册的iOS页面,如果键盘已启用,我想让它可滚动,因为此刻我无法滚动到页面末尾的注册按钮,键盘会隐藏按钮.

有智能解决方案吗?

解决方法

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

如果您想使用UIScrolView,您应该将UIView注册表格插入到UIScrolView并设置内容大小.

代码狙击手如何处理键盘和scrollview.

首先,您应该知道何时显示键盘并隐藏.使用通知

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

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

}

之后使用方法(keyboardWasShown和keyboardWillBeHidden)的通知来改变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参数一样更改框架或约束.

猜你在找的Xcode相关文章