Skip to content

Instantly share code, notes, and snippets.

@gabro
Created July 17, 2015 09:41
Show Gist options
  • Save gabro/eeba244f6c418e1df168 to your computer and use it in GitHub Desktop.
Save gabro/eeba244f6c418e1df168 to your computer and use it in GitHub Desktop.
Handle viewWillTransitionToSize across all `UIViewController`s
// UIViewController+BackgroundStatusBar.swift
// hailadoc-ios
//
// Created by Gabriele Petronella on 7/16/15.
// Copyright (c) 2015 buildo. All rights reserved.
//
import Foundation
extension UIViewController {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
// make sure this isn't a subclass
if self !== UIViewController.self {
return
}
dispatch_once(&Static.token) {
let originalSelector = Selector("viewWillTransitionToSize:withTransitionCoordinator:")
let swizzledSelector = Selector("hd_viewWillTransitionToSize:withTransitionCoordinator:")
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
}
func hd_viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.hd_viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
let isPortrait = size.width < size.height
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
UIView.animateWithDuration(0.3) {
appDelegate.statusBarBackground.alpha = isPortrait ? 1 : 0
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment