Skip to content

Instantly share code, notes, and snippets.

UIView *getStatusBar() {
UIView *subViewWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
for (UIView *i in subViewWindow.subviews) {
if ([i isKindOfClass:NSClassFromString(@"UIStatusBar")]) {
return i;
}
}
return nil;
}
@wh1pch81n
wh1pch81n / objectSetUpInBlock.swift
Created April 17, 2016 19:20
Sometimes it is easier to read code where the set up is in a block.
import UIKit
/**
You can use this to initalize an object (class or struct) while containing the set up code for it in a block
*/
@warn_unused_result
func setUpObject<T>(obj: T, withBlock setUp: (inout T) -> ()) -> T {
var _obj = obj
setUp(&_obj)
return _obj
@wh1pch81n
wh1pch81n / ClickableLinksUITextView
Created April 14, 2016 03:45
UITextView supports clinking a link and going directly to the website, but sometimes you want that "link" to act like a button so you can do something else when you click it. In this example we have ""no-opclickno-op"" where "click" is a link that will print "hello world"
import UIKit
import XCPlayground
class ClickableLinkTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
setUp()
}
required init?(coder aDecoder: NSCoder) {
@wh1pch81n
wh1pch81n / TypeInferedVCFromStoryboard
Created March 31, 2016 14:34
In swift, if you instantiate the view controller from the storyboard identifier, you typically need to cast it. However, since the storyboard identifier is typically the same as the vc name, it might be perfectly acceptable to just pass in the type and convert that to a string. Since you passed in a type, the function can infer the return type.
extension UIStoryboard {
func instantiateViewControllerWithTypeAsIdentifier<T>(t: T.Type) -> T {
return instantiateViewControllerWithIdentifier(String(t)) as! T
}
}
import AVFoundation
class EACAudioManager: NSObject {
static let sharedInstance = EACAudioManager()
var volumeDidChange = { (newVolume: Float) -> () in }
var currentVolume: Float { return AVAudioSession.sharedInstance().outputVolume }
override init() {
super.init()
try! AVAudioSession.sharedInstance().setActive(true)
AVAudioSession.sharedInstance().addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.New, context: nil)