56

I have a view controller that takes up the whole screen from top to bottom. I would like to hide the home bar indicator on the bottom of the screen on iPhone X devices.

How can I do this in iOS 11?

Vlad
  • 5,727
  • 3
  • 38
  • 59

6 Answers6

86

You should override prefersHomeIndicatorAutoHidden in your view controller to achieve that:

override var prefersHomeIndicatorAutoHidden: Bool {
    return true
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 2
    But that slide twice is not working, I am testing in emulator. Even if its hidden, on single swipe its going on home screen. – smit patel Nov 16 '17 at 04:56
  • 1
    @smitpatel _When auto-hiding is enabled, the indicator fades out if the user hasn't touched the screen for a few seconds. It reappears when the user touches the screen again. This behavior should be enabled only for passive viewing experiences like playing videos or photo slideshows_ – Jack Nov 17 '17 at 03:38
  • 5
    "You'll actually have to slide twice to activate the gesture." This just isn't true. You have to slide twice if you override `preferredScreenEdgesDeferringSystemGestures`, not if you override `prefersHomeIndicatorAutoHidden`. – Tim Vermeulen Nov 29 '18 at 11:39
29

There is another alternative. If you are looking for the behavior where the indicator dims, then when the user swipes up it activates, and when they swipe up again the home action is invoked (I.E., two swipes are needed to invoke), then the answer is here: iPhone X home indicator behavior. The short of it is to override on your UIViewController:

override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge {
    return UIRectEdge.bottom
}

prefersHomeIndicatorAutoHidden only hides the indicator, but will not suppress the gesture.

And you will get what you want (If I understand your comments correctly - your question and the selected answer seem to imply the other answer).

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
absmiths
  • 1,144
  • 1
  • 12
  • 21
  • 1
    This is what i wanted to achive. Rest all just hides the home indicator. – Ankit Kumar Gupta Jan 08 '19 at 09:20
  • I have an 'Method does not override any method from its superclass' error – Zaporozhchenko Oleksandr Aug 22 '19 at 12:58
  • The documentation says you also need to call `setNeedsUpdateOfScreenEdgesDeferringSystemGestures()` if you change the value. – Victor Engel Aug 23 '20 at 23:11
  • It seems like if you set the prefersHomeIndicatorAutoHidden with return true in addition with preferredScreenEdgesDeferringSystemGestures, the double swipe-up has no effect. With the prefersHomeIndicatorAutoHidden out-commented it works fine. Is there a solution to completely hide the home indicator and the double swipe-up enabled? – ZAY Nov 16 '22 at 10:22
6

If your window?.rootViewController is a UITabBarController or UINavigationController, just inherit it and add two function as follows,

override var prefersHomeIndicatorAutoHidden: Bool {
    return true
}

//@available(iOS 11, *)
override var childViewControllerForHomeIndicatorAutoHidden: UIViewController? {
    return nil
}
luismgb
  • 81
  • 1
  • 8
DawnSong
  • 4,752
  • 2
  • 38
  • 38
4

I tried to set it and return true only when I am in full-screen :

override var prefersHomeIndicatorAutoHidden: Bool { isNavigationBarAndTabBarHidden }

but it doesn't seem to work... isNavigationBarAndTabBarHidden is a custom variable tied to my fullscreen extension.

Edit: We need to call setNeedsUpdateOfHomeIndicatorAutoHidden every time we update prefersHomeIndicatorAutoHidden's value.

    var isNavigationBarAndTabBarHidden = false {
        didSet {
            setNeedsUpdateOfHomeIndicatorAutoHidden()
        }
    }
Mycroft Canner
  • 1,828
  • 1
  • 11
  • 24
3

Implement -(BOOL)prefersHomeIndicatorAutoHidden in your UIViewController and return YES.

Read more https://developer.apple.com/documentation/uikit/uiviewcontroller/2887510-prefershomeindicatorautohidden.

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67
1
override func  prefersHomeIndicatorAutoHidden() -> Bool {
    return true
}

I suppose you can add this method in your AppDelegate for hide home indicator on all of your ViewControllers.

enter image description here

pierreafranck
  • 445
  • 5
  • 19