【问题标题】:iOS7 UIScrollView contentInset not workingiOS7 UIScrollView contentInset 不起作用
【发布时间】:2013-10-04 14:45:12
【问题描述】:

当键盘被隐藏时,滚动视图应该回到它的原始 contentInset,但它在 iOS7 中不起作用。在显示键盘时设置滚动视图的 contentInset 是有效的,但是当键盘被隐藏时,滚动视图的 contentInset 不能设置为零。 代码:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notif
{
    CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);


    UIScrollView *scrollView = (UIScrollView *)self.view;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    CGRect rect = self.view.frame;
    rect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(rect, self.wishContentField.frame.origin)) {
        CGPoint point = CGPointMake(0, self.wishContentField.frame.origin.y - keyboardSize.height);
        [scrollView setContentOffset:point animated:YES];
    }

}
- (void)keyboardWasHidden:(NSNotification *)notif
{
    UIEdgeInsets zeroInsets = UIEdgeInsetsZero;
    UIScrollView *scrollView = (UIScrollView *)self.view;
    [scrollView setContentInset:zeroInsets];
    scrollView.scrollIndicatorInsets = zeroInsets;
}

【问题讨论】:

  • 你能解释一下它是如何不工作的(键盘隐藏后它是如何工作的)吗?请注意,在 iOS 7 上,如果您有一个半透明的 navigationBar,您的视图控制器将在您的滚动视图中设置一个顶部插图(如果没有另外设置)。这里可能就是这种情况,因为你设置的是contentInset.top=0,所以它可能会隐藏navigationBarstatusBar后面的一些内容。
  • 感谢您的回复。我将顶部设置为 navigationBar.frame.size.height,它现在可以工作了。
  • 写一个答案并给 alex-i 一些信任 ;)

标签: ios uiscrollview ios7


【解决方案1】:

我还发现,如果您将新的 contentinset 设置为与现有 inset 完全相同,则滚动视图可能会忽略它并恢复为零 inset。因此,一个简单的解决方法是检查您设置的新 contentinset 与上一个是否至少相差 1 点。

【讨论】:

    【解决方案2】:

    将 contentOffset 设置为零。无论您的滚动视图是在导航控制器内还是其他任何情况下,它都适用。在下面找到相同的代码 sn-p:

    - (void)keyboardWasHidden:(NSNotification *)notif
    {    
        UIScrollView *scrollView = (UIScrollView *)self.view;
        scrollView.contentOffset = CGPoint.zero
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      self.automaticallyAdjustsScrollViewInsets = NO;

      这对我有用...

      【讨论】:

        【解决方案4】:

        这可能与 contentSize 不起作用有关 除非在 VC 中设置

        - (void)viewDidLayoutSubviews
        {
             self.scrollView.contentSize = whatever
        }
        

        只是说你的头可能撞错了墙

        【讨论】:

          【解决方案5】:

          所以,仅仅因为我仍然发现这个答案很有用,这就是我所做的。我接受了@alex-i 的建议和@yong-ho 的评论。但是由于某种原因,导航栏的高度不够。

          UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height + 20.0f, 0.0, 0.0, 0.0); 
          scrollView.contentInset = contentInsets;
          scrollView.scrollIndicatorInsets = contentInsets;
          

          就像我说的,我必须添加 20.0f 否则我的内容仍然会被截断。不知道为什么。如果我弄清楚了,我会更新我的答案。

          【讨论】:

          • 我也遇到过这个问题,我必须向 contentInsets 添加大约 90 个,但 scrollIndicatorInsets 工作正常。你使用自动调整大小吗?
          • 永远不要使用这样的幻数 (20.0f)。直接获取状态栏高度。例如,iPhone X 的状态栏高度发生了变化,或者在拨打40.0f40.0f时在早期设备上发生了变化
          猜你喜欢
          • 2014-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-21
          • 2023-03-25
          • 2017-01-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多