简体   繁体   English

iOS7 UIScrollView contentInset 不起作用

[英]iOS7 UIScrollView contentInset not working

When the keyboard was hidden, the scrollview should back to it's origin contentInset, but it's not working in iOS7.当键盘被隐藏时,滚动视图应该回到它的原始 contentInset,但它在 iOS7 中不起作用。 Setting scrollview's contentInset when keyboard was shown is working but when the keyboard was hidden, the scrollview's contentInset can't set to inset zero.显示键盘时设置滚动视图的 contentInset 正在工作,但是当键盘隐藏时,滚动视图的 contentInset 不能设置为插入零。 The code:代码:

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

Try this one:试试这个:

self.automaticallyAdjustsScrollViewInsets = NO;

This is working for me...这对我有用...

it might be related to contentSize not working except when set in VCs它可能与 contentSize 不起作用有关,除非在 VC 中设置

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

just saying you might be smashing your head against the wrong wall只是说你可能把头撞到了错误的墙上

So, just because I still found this answer useful, here is what I did.所以,仅仅因为我仍然发现这个答案有用,这就是我所做的。 I took the advice of @alex-i and the comment by @yong-ho.我接受了@alex-i 的建议和@yong-ho 的评论。 But for some reason the height of the navigation bar wasn't quite enough.但由于某种原因,导航栏的高度不够。

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

Like I said, I had to add that 20.0f or my content was still cut off.就像我说的,我必须添加 20.0f 否则我的内容仍然被切断。 Not sure why.不知道为什么。 If I figure it out I'll update my answer.如果我弄清楚了,我会更新我的答案。

Set contentOffset to zero.将 contentOffset 设置为零。 It will work in all cases no matter your scroll view is inside navigation controller or any other.无论您的滚动视图是在导航控制器内还是任何其他控制器,它都适用于所有情况。 Find below the code snippet for same:在下面找到相同的代码片段:

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

I also found that if you set a new contentinset exactly the same as the existing inset, the scrollview may ignore it and revert to a zero inset.我还发现,如果您将新的 contentinset 设置为与现有 inset 完全相同,则滚动视图可能会忽略它并恢复为零 inset。 So an easy workaround is to check that the new contentinset you are setting is at least 1 point different than the last.因此,一个简单的解决方法是检查您正在设置的新 contentinset 与上一个是否至少有 1 点不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM