iOS 7 — navigationController is setting the contentInset and ContentOffset of my UIScrollView

ぐ巨炮叔叔 提交于 2019-11-26 18:28:43
KDaker

Try setting self.automaticallyAdjustsScrollViewInsets = NO in your main view controller.

This was introduced in iOS 7 so you might want to wrap that with an iOS version check, if you are supporting iOS 6 and below.


Update

If you are using storyboards, you can do this in the Interface Builder as well as by checking 'Adjust Scroll View Insets' for your selected controller.

I had a similar problem, after dismissing a viewController, the contentOffset from my tableView was changed to (0, -64).

my solution was a little weird, I tried all the other answers but had no success, the only thing that fixed my problem was to switch the tableView position in the controls tree of the .xib

it was the first control in the parent View like this:

I moved the tableView right after the ImageView and it worked:

it seems that putting the table view in the first position was causing the trouble, and moving the table view to another position fixed the problem.

P.D. I'm not using autoLayout neither storyboards

hope this can help someone!

I have two solutions:

1.

self.view = scrollView;

2.

[self.navigationController.toolbar setTranslucent:NO];
Savvy iPhone

I'm having the same problem.

  1. Setting self.automaticallyAdjustsScrollViewInsets = NO solved the issue for some of the view but not everywhere.

  2. Second solution is to set the content-offset of tableview/view/scrollview in viewWillLayoutSubviews:

    - (void)viewWillLayoutSubviews {
        //Arrange the view
        CGRect tempViewFrame = self.view.frame;
    
        if (tempViewFrame.origin.y == 64.0f) {
            tempViewFrame.origin.y = 0;
            self.view.frame = tempViewFrame;
        }
    }
    

This is enough

- (void)viewDidLoad {

    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;

This fixes the issue on both cases when:

  1. Showing Status Bar
  2. Showing Status Bar + Navigation Bar

    override func viewDidLayoutSubviews() {
            NSLog("ORIGIN: \(self.view.frame.origin.y)")
    
        if self.view.frame.origin.y == 0 {
            if let rect = self.navigationController?.navigationBar.frame {
                let y = rect.size.height + rect.origin.y
                self.tableView.contentInset = UIEdgeInsetsMake(y, 0, 0, 0)
            }
        } else if self.view.frame.origin.y == 44 || self.view.frame.origin.y == 64 {
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    

The other answers may work for you, but they didn't work for me. What worked for me was to manually set the y property of the contentOffset in viewDidLayoutSubviews and viewWillAppear:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Set the offset to go below the status bar
    collectionView.contentOffset.y = -UIApplication.shared.statusBarFrame.height
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Set the offset to go below the status bar
    collectionView.contentOffset.y = -UIApplication.shared.statusBarFrame.height
}

In my case, my controller had a child collection view which would sometimes get the contentOffset adjusted and other times not. The way I normalized it was to just manually adjust it every time. I wanted the content to be offset to below the status bar, so I used the status bar height as the value (negative because I want the content to be pushed down).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!