【问题标题】:Trying to limit scroll area inside a UIScrollView when displaying the keyboard after a UITextField becomes active in iOS在 iOS 中 UITextField 变为活动状态后显示键盘时尝试限制 UIScrollView 内的滚动区域
【发布时间】:2015-11-13 13:36:32
【问题描述】:

我正在做一个项目,我有一个 UIView,其中包含多个 UI 元素,包括一个 UITextField。一旦UITextField 激活(即becomesFirstResponder),弹出的键盘将覆盖UITextField。包含所有 UI 元素的 UIView 位于 UIScrollView 中。

现在,除了将 contentView 上移到 UIScrollView 的可见区域之外,我需要做的另一件事是确保用户无法滚动键盘下方(和后面)的 contentView,也无法滚动 contentView上方(和导航栏后面)。

我目前的相关代码是:

-(void)keyboardDidShow:(NSNotification *)notification {
    CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    self.scrollViewBottomContstraint.constant = height;
    [self.view layoutIfNeeded];

}

谁能告诉我如何确保我的 contentView 只能在导航栏和键盘之间的可见区域内最小限度地滚动?

【问题讨论】:

    标签: ios uiscrollview autolayout uitextfield


    【解决方案1】:

    这可能对你有帮助。

    //KEYBOARD SHOWN
        - (void)keyboardShown:(NSNotification*)notification
        {
            NSDictionary* dic = [notification userInfo];
            CGSize keyboardSize = [[dic objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
            UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
            scrollView.contentInset = contentInsets;
            scrollView.scrollIndicatorInsets = contentInsets;
    
            CGPoint scrollPoint = CGPointMake(0.0, textField.frame.origin.y-keyboardSize.height);
            [scrollView setContentOffset:scrollPoint animated:YES];
    
        }
    
          //when keyboard hides
        - (void)keyboardHidden:(NSNotification*)notification
        {
            // Set the Scroll view content inset to UIEdgeInsetsZero
        }
    

    【讨论】:

      【解决方案2】:

      当键盘通知出现时,我会:

      1. 更改UIScrollView 上的约束常量,使其大小正好位于屏幕的可见部分。您已经使用底部约束的高度设置开始了这条路径。
      2. 更改UIScrollView 上的contentSize 属性以匹配。这样,用户执行的任何滚动操作都仅限于他们可以看到的内容。

      当键盘消失时,您可以恢复到之前的设置,然后再进行这些调整。

      请注意,在某些情况下,使用带有静态表格单元格和其中的静态内容的 UITableView 会更容易。 iOS 运行时似乎处理了一些对您来说可见的滚动内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-27
        • 1970-01-01
        • 2021-09-01
        • 2012-09-25
        • 1970-01-01
        • 2013-03-25
        相关资源
        最近更新 更多