-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnimatableStatusBarViewController.swift
78 lines (63 loc) · 3.05 KB
/
AnimatableStatusBarViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// AnimatableStatusBarViewController.swift
// AnimatableStatusBarViewController
//
// Created by Wirawit Rueopas on 21/4/18.
// Copyright © 2018 Wirawit Rueopas. All rights reserved.
//
import UIKit
/// Base View Controller that animates status bar changes. Make your view controller override from this instead of `UIViewController`.
///
/// **To use:**
/// 1. Set `animatedStatusBarPreviouslyHideStatusBar` on creation time.
/// 2. Override `animatedStatusBarPrefersStatusBarHidden` (instead of `prefersStatusBarHidden`)
/// 3. Override `animatedStatusBarAnimationDuration`
class AnimatableStatusBarViewController: UIViewController {
/// For animating status bar when presenting this vc
private var shouldCurrentlyHideStatusBar: Bool = false
/// Set initial stage whether previous view controller hides status bar.
var animatedStatusBarPreviouslyHideStatusBar: Bool = false {
didSet {
shouldCurrentlyHideStatusBar = animatedStatusBarPreviouslyHideStatusBar
}
}
/// Use this instead of the original `prefersStatusBarHidden` to indicate whether this view controller needs status bar hidden or not.
var animatedStatusBarPrefersStatusBarHidden: Bool {
return false
}
var animatedStatusBarAnimationDuration: Double {
return 0.4
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// If not the same, animate change
if animatedStatusBarPreviouslyHideStatusBar != animatedStatusBarPrefersStatusBarHidden {
shouldCurrentlyHideStatusBar = animatedStatusBarPrefersStatusBarHidden
UIView.animate(withDuration: animatedStatusBarAnimationDuration) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
if shouldCurrentlyHideStatusBar != animatedStatusBarPrefersStatusBarHidden {
shouldCurrentlyHideStatusBar = animatedStatusBarPrefersStatusBarHidden
UIView.animate(withDuration: animatedStatusBarAnimationDuration) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Will present another animated, set status bar to that style,
// *so that when we dismiss that one, this one will initially has the same style as that one. (then will be animated to current style)
if let presentedVc = presentedViewController as? AnimatableStatusBarViewController {
shouldCurrentlyHideStatusBar = presentedVc.animatedStatusBarPrefersStatusBarHidden
} else if let topOfNavVc = (presentedViewController as? UINavigationController)?.visibleViewController as? AnimatableStatusBarViewController {
shouldCurrentlyHideStatusBar = topOfNavVc.animatedStatusBarPrefersStatusBarHidden
}
}
override var prefersStatusBarHidden: Bool {
return shouldCurrentlyHideStatusBar
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return UIStatusBarAnimation.fade
}
}