Skip to content

Instantly share code, notes, and snippets.

@dcramps
Last active August 17, 2017 16:01
Show Gist options
  • Save dcramps/bfa62fc0c8c59af230936300b67dfa74 to your computer and use it in GitHub Desktop.
Save dcramps/bfa62fc0c8c59af230936300b67dfa74 to your computer and use it in GitHub Desktop.
Use this class if you're a horrible person that wants to change the colour of an iOS status bar. If the status bar changes in any way, you have to do it again. Have fun.
final class TintMyStatusBar {
static func to(_ colour: UIColor) {
let sbw = UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow
for statusbar in sbw.subviews {
for bgfg in statusbar.subviews {
for itemView in bgfg.subviews {
let image = itemView.layer.contents as! CGImage
itemView.layer.contents = self.tint(image: image, toColour: colour)
}
}
}
}
private static func tint(image: CGImage, toColour colour: UIColor) -> CGImage {
let maskImage = image
let width = image.width
let height = image.height
let bounds = CGRect(x: 0, y: 0, width: width, height: height)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let bitmapContext = CGContext(
data: nil,
width: Int(width),
height: Int(height),
bitsPerComponent: 8,
bytesPerRow: 0,
space: colorSpace,
bitmapInfo: bitmapInfo.rawValue)
bitmapContext?.clip(to: bounds, mask: maskImage)
bitmapContext?.setFillColor(colour.cgColor)
bitmapContext?.fill(bounds)
let cgImage = bitmapContext?.makeImage()
return cgImage!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment