Skip to content

Commit 752e1e6

Browse files
authored
Merge pull request #133 from renanwillamy/hotfix_rotation
keep the sideMenu open on rotation
2 parents aaa8333 + 236f64f commit 752e1e6

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

Sources/SideMenu/Preferences.swift

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ extension SideMenuController {
123123

124124
/// The sensitivity of the pan gesture recognizer revealing menu view controller.
125125
public var panGestureSensitivity: CGFloat = 0.25
126+
127+
/// If the side menu should keep open on rotation. Default is false.
128+
public var shouldKeepMenuOpen: Bool = false
126129
}
127130

128131
/// The basic configuration of side menu.

Sources/SideMenu/SideMenuController.swift

+39-9
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,32 @@ open class SideMenuController: UIViewController {
711711
}
712712
}
713713

714+
private func updateSideMenuWidth() {
715+
let fifteenPercent = 0.15
716+
let size = UIScreen.main.bounds.size
717+
SideMenuController.preferences.basic.menuWidth = size.width - (size.width * CGFloat(fifteenPercent))
718+
}
719+
720+
private func keepSideMenuOpenOnRotation() {
721+
if menuViewController != nil {
722+
if self.isMenuRevealed {
723+
self.hideMenu(animated: false, completion: nil)
724+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
725+
self.updateSideMenuWidth()
726+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
727+
self.revealMenu(animated: false, completion: nil)
728+
})
729+
})
730+
} else {
731+
self.revealMenu(animated: false) { _ in
732+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
733+
self.hideMenu(animated: false, completion: nil)
734+
})
735+
}
736+
}
737+
}
738+
}
739+
714740
// MARK: Orientation
715741

716742
open override var shouldAutorotate: Bool {
@@ -728,16 +754,20 @@ open class SideMenuController: UIViewController {
728754
}
729755

730756
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
731-
hideMenu(animated: false, completion: { _ in
732-
// Temporally hide the menu container view for smooth animation
733-
self.menuContainerView.isHidden = true
734-
coordinator.animate(alongsideTransition: { _ in
735-
self.contentContainerView.frame = self.contentFrame(visibility: self.isMenuRevealed, targetSize: size)
736-
}, completion: { (_) in
737-
self.menuContainerView.isHidden = false
738-
self.menuContainerView.frame = self.sideMenuFrame(visibility: self.isMenuRevealed, targetSize: size)
757+
if preferences.basic.shouldKeepMenuOpen {
758+
self.keepSideMenuOpenOnRotation()
759+
} else {
760+
hideMenu(animated: false, completion: { _ in
761+
// Temporally hide the menu container view for smooth animation
762+
self.menuContainerView.isHidden = true
763+
coordinator.animate(alongsideTransition: { _ in
764+
self.contentContainerView.frame = self.contentFrame(visibility: self.isMenuRevealed, targetSize: size)
765+
}, completion: { (_) in
766+
self.menuContainerView.isHidden = false
767+
self.menuContainerView.frame = self.sideMenuFrame(visibility: self.isMenuRevealed, targetSize: size)
768+
})
739769
})
740-
})
770+
}
741771

742772
super.viewWillTransition(to: size, with: coordinator)
743773
}

0 commit comments

Comments
 (0)