Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 UIScrollView show offset content below status bar

I'm developing my app to work with iOS7. I have a UINavigationController I'm pushing a UIViewController that has a ScrollView inside it. Inside the scrollView I have a tableView. Is it possible to achieve that when I scroll the tableView inside the scrollView the list will appear behind that Status bar. Same why it would be if I had a UINavigationController and a UIViewController with a tableView in it.

So this it the hierarchy :

UINavigationController -> UIViewController -> UIScrollView -> UITableView .

and I want that when a user scroll the table,the cells in the top will be visible under the status bar.

If there is no UIScrollView it happens automatically in iOS7.

Thanks.

like image 415
user958880 Avatar asked Nov 06 '13 15:11

user958880


6 Answers

Just set automaticallyAdjustsScrollViewInsets to NO in the viewController init method.

In Storyboard, you can switch the property directly in the property panel when the UIViewController is selected.

If you use xib, just set it like this:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self.automaticallyAdjustsScrollViewInsets = NO;
}

Note: this is right since iOS7 and still in iOS8.

like image 119
LeChatNoir Avatar answered Nov 19 '22 00:11

LeChatNoir


none of the above workd for me, until I noticed that I had to set Content Insets from Automatically to Never in the interfacebuilder:

enter image description here

like image 36
MQLN Avatar answered Nov 19 '22 01:11

MQLN


Starting with iOS 11 you can use this new property with a fallback (Swift 4):

if #available(iOS 11.0, *) {
    scrollView.contentInsetAdjustmentBehavior = .never
} else {
    self.automaticallyAdjustsScrollViewInsets = false
}
like image 26
Skoua Avatar answered Nov 19 '22 01:11

Skoua


The answer from Skoua might work in some situations, but does have certain side-effects on iOS11 and later. Most notably, the scroll view will start propagating safe areas to its children, which can mess up your layout while scrolling if you use the safe areas or layout margins.

Apple explains this very well and in detail in this WWDC session and also mentions explicitly that contentInsetAdjustmentBehavior = .never can have side-effects and is not what you should use in most cases.


To get a scroll view that does not mess up our layout, but shows its content below the status bar (or navigation bar), you should observe the safe area of your scroll view and adjust your custom content insets accordingly:

private var scrollViewSafeAreaObserver: NSKeyValueObservation!

override func viewDidLoad() {

    ...

    if #available(iOS 11.0, *) {
        self.scrollViewSafeAreaObserver = self.scrollView.observe(\.safeAreaInsets) { [weak self] (_, _) in
            self?.scrollViewSafeAreaInsetsDidChange()
        }
    } else {
        self.automaticallyAdjustsScrollViewInsets = false
    }
}

@available(iOS 11.0, *)
func scrollViewSafeAreaInsetsDidChange() {
    self.scrollView.contentInset.top = -self.scrollView.safeAreaInsets.top
}

deinit {
    self.scrollViewSafeAreaObserver?.invalidate()
    self.scrollViewSafeAreaObserver = nil
}

Why does this work? Because we leave contentInsetAdjustmentBehavior = .automatic. This will give us normal behaviour when it comes to scrolling and non-scrolling axis, but the UIScrollView will also "convert" any safe areas to content insets. Since we don't want this for our top edge, we simply set the negative top safe area as our custom insets, which will counter any insets set by the scroll view.

like image 44
BlackWolf Avatar answered Nov 19 '22 01:11

BlackWolf


Thats just dumb from Apple. One more weird behaviour to worry about. Anyhow, I ended up setting the scroll view content inset for top to -20 (from IB).

like image 3
codrut Avatar answered Nov 19 '22 01:11

codrut


I found the solution! Just set:

self.automaticallyAdjustsScrollViewInsets = false

on the view controller that has the UIScrollView.

like image 1
Codetard Avatar answered Nov 19 '22 02:11

Codetard