Last active
September 13, 2016 19:03
-
-
Save pablogm/8906e68d950747888343 to your computer and use it in GitHub Desktop.
Swift UIViewController utils: custom status bar background color, popup alert message, ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIViewController+StatusBar.swift | |
// Swift Extensions | |
// | |
import Foundation | |
import UIKit | |
/// Custom status bar background color on UIViewController | |
extension UIViewController { | |
public override class func initialize() { | |
struct Static { | |
static var token: dispatch_once_t = 0 | |
} | |
if self !== UIViewController.self { | |
return | |
} | |
dispatch_once(&Static.token) { | |
let originalSelector = Selector("viewDidLoad") | |
let swizzledSelector = Selector("customViewDidLoad") | |
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); | |
} | |
} | |
} | |
// MARK: - Method Swizzling | |
func customViewDidLoad() { | |
self.customViewDidLoad() | |
// Hide navigation bar | |
self.navigationController?.setNavigationBarHidden(true, animated: true) | |
statusBarBackgroundColor(0x0b6e99) | |
} | |
// MARK: - Custom statusBar background color | |
func statusBarBackgroundColor(color: Int) { | |
let view = UIView(frame: | |
CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0) | |
) | |
view.backgroundColor = UIColor.init(hex: color) | |
self.view.addSubview(view) | |
} | |
} | |
/// Popup alert from view controller | |
extension UIViewController { | |
func showAlert(title: String, message: String, ok: String, cancel: String, | |
cancelAction: CancelActionPerformed?, | |
okAction: OkActionPerformed?, | |
completion: AlertPerformed?) { | |
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) | |
if cancelAction != nil { | |
let cancelAction = UIAlertAction(title: cancel, style: .Cancel) { (action) in | |
if let a = cancelAction { | |
a(action) | |
} | |
} | |
alertController.addAction(cancelAction) | |
} | |
let OKAction = UIAlertAction(title: ok, style: .Default) { (action) in | |
if let a = okAction { | |
a(action) | |
} | |
} | |
alertController.addAction(OKAction) | |
self.presentViewController(alertController, animated: true) { | |
if let a = completion { | |
a() | |
} | |
} | |
} | |
func presentDefaultAlert(alertText: String?, subText: String?, dismissText: String) -> UIAlertController { | |
let alert = UIAlertController(title: alertText, message: subText, preferredStyle: .Alert) | |
let action = UIAlertAction(title: dismissText, style: UIAlertActionStyle.Default) { (action) -> Void in | |
} | |
alert.addAction(action) | |
self.presentViewController(alert, animated: true, completion: .None) | |
return alert | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment