Skip to content

Instantly share code, notes, and snippets.

@vinhnx
Created June 25, 2016 09:13
Show Gist options
  • Save vinhnx/19b8115927bf5ba01417c00cd527ebc2 to your computer and use it in GitHub Desktop.
Save vinhnx/19b8115927bf5ba01417c00cd527ebc2 to your computer and use it in GitHub Desktop.
ios autorotation + status bar boilerplate for swift
public class ViewController : UIViewController
{
#if os(iOS)
public override func shouldAutorotate() -> Bool {
return false
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
public override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return .Portrait
}
public override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
#endif
}
public class NavigationController : UINavigationController, UIGestureRecognizerDelegate
{
#if os(iOS)
override func viewDidLoad() {
super.viewDidLoad()
self.interactivePopGestureRecognizer?.delegate = self
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return self.topViewController == self.viewControllers.last && self.viewControllers.count > 1
}
override func shouldAutorotate() -> Bool {
if let shouldRotate = self.topViewController?.shouldAutorotate() {
return shouldRotate
}
return super.shouldAutorotate()
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if let orientation = self.topViewController?.supportedInterfaceOrientations() {
return orientation
}
return super.supportedInterfaceOrientations()
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
if let orientation = self.topViewController?.preferredInterfaceOrientationForPresentation() {
return orientation
}
return super.preferredInterfaceOrientationForPresentation()
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
if let style = self.topViewController?.preferredStatusBarStyle() {
return style
}
return super.preferredStatusBarStyle()
}
#endif
}
public extension UINavigationController
{
public func pushViewController(viewController: UIViewController, animated: Bool, completion: VoidBlock) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.pushViewController(viewController, animated: animated)
CATransaction.commit()
}
}
public class TabBarController : UITabBarController
{
#if os(iOS)
public override func shouldAutorotate() -> Bool {
if let viewController = self.viewControllers?[self.selectedIndex] {
return viewController.shouldAutorotate()
}
return super.shouldAutorotate()
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if let viewController = self.viewControllers?[self.selectedIndex] {
return viewController.supportedInterfaceOrientations()
}
return super.supportedInterfaceOrientations()
}
public override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
if let viewController = self.viewControllers?[self.selectedIndex] {
return viewController.preferredInterfaceOrientationForPresentation()
}
return super.preferredInterfaceOrientationForPresentation()
}
public override func preferredStatusBarStyle() -> UIStatusBarStyle {
if let viewController = self.viewControllers?[self.selectedIndex] {
return viewController.preferredStatusBarStyle()
}
return super.preferredStatusBarStyle()
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment