Skip to content

Instantly share code, notes, and snippets.

@filletofish
Created November 23, 2017 09:17
Show Gist options
  • Save filletofish/8586d47ac48797af243e222208fcfad2 to your computer and use it in GitHub Desktop.
Save filletofish/8586d47ac48797af243e222208fcfad2 to your computer and use it in GitHub Desktop.
🌫️ Blurred Status Bar View
//
// BlurredStatusBarView.swift
//
// Created by Filipp Fediakov on 23.11.17.
// Copyright © 2017 filletofish. All rights reserved.
//
import UIKit
class BlurredStatusBarView: UIView {
private var effectView: UIVisualEffectView
init(style: UIBlurEffectStyle) {
let width = UIScreen.main.bounds.width
let height = UIApplication.shared.statusBarFrame.height
let blurEffect = UIBlurEffect(style: .dark)
effectView = UIVisualEffectView(effect: blurEffect)
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let vibrancyView = UIVisualEffectView(effect: vibrancyEffect)
super.init(frame: CGRect(x: 0, y: 0, width: width, height: height))
effectView.frame = self.bounds
vibrancyView.frame = self.bounds
self.addSubview(effectView)
self.addSubview(vibrancyView)
NotificationCenter.default.addObserver(self,
selector: #selector(statusBarDidRotated(_:)),
name: NSNotification.Name.UIApplicationWillChangeStatusBarOrientation,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(statusBarDidChangeFrame(_:)),
name: NSNotification.Name.UIApplicationWillChangeStatusBarFrame,
object: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func statusBarDidRotated(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as? [String: Any] else { return }
guard let rawOrientation = userInfo[UIApplicationStatusBarOrientationUserInfoKey] as? Int,
let orientation = UIInterfaceOrientation(rawValue: rawOrientation) else {
return
}
if orientation == .portrait || orientation == .portraitUpsideDown {
UIView.animate(withDuration: 0.0, animations: { self.alpha = 1})
} else {
UIView.animate(withDuration: 0.0, animations: { self.alpha = 0})
}
}
@objc func statusBarDidChangeFrame(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as? [String: Any] else { return }
guard let frame = (userInfo[UIApplicationStatusBarFrameUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
UIView.animate(withDuration: 0.4, animations: {
self.frame = CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y,
width: frame.width, height: frame.height)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment