Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created September 19, 2017 15:07
Show Gist options
  • Save christianselig/54c0e76c65a46e233c9be049c727e1c1 to your computer and use it in GitHub Desktop.
Save christianselig/54c0e76c65a46e233c9be049c727e1c1 to your computer and use it in GitHub Desktop.
//
// ApolloActivityViewController.swift
// Apollo
//
// Created by Christian Selig on 2016-12-12.
// Copyright © 2016 Christian Selig. All rights reserved.
//
import UIKit
/// Modification of `UIActivityViewController` that accomplishes Dark Mode support via a bunch of hacks, view sniffing and view traversing.
class ApolloActivityViewController: UIActivityViewController {
var darkeningView: UIView?
var settingUpDarkeningView: Bool = true
override func viewDidLoad() {
super.viewDidLoad()
if ThemeManager.shared.currentTheme == .dark {
// Add a background view that causes the UIVisualEffectView to be a little darker
let darkeningView = UIView()
darkeningView.alpha = 0.0
darkeningView.layer.masksToBounds = true
darkeningView.backgroundColor = UIColor.black.withAlphaComponent(0.3)
self.darkeningView = darkeningView
// Constantly refresh the views in case AirDrop finds new people to share to and we'll need to color their labels
constantlyRefresh()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let darkeningView = darkeningView {
view.superview?.addSubview(darkeningView)
view.superview?.sendSubview(toBack: darkeningView)
darkeningView.layer.cornerRadius = view.layer.cornerRadius
// Dispatch ahead one run cycle in order to keep things in sync
DispatchQueue.main.async {
self.settingUpDarkeningView = false
UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveEaseInOut, animations: {
darkeningView.frame.origin.y = self.view.frame.origin.y
darkeningView.alpha = 1.0
}, completion: nil)
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
self.darkeningView?.alpha = 0.0
}, completion: { (finished) in
self.darkeningView?.removeFromSuperview()
})
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
darkeningView?.frame = view.frame
if ThemeManager.shared.currentTheme == .dark, let superview = view.superview {
colorSubviewsOfView(superview)
}
if settingUpDarkeningView, let darkeningView = darkeningView {
darkeningView.frame.origin.y = view.frame.origin.y + darkeningView.frame.height
}
}
func constantlyRefresh() {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250)) {
if let superview = self.view.superview {
self.colorSubviewsOfView(superview)
}
self.constantlyRefresh()
}
}
fileprivate func colorSubviewsOfView(_ view: UIView) {
if let visualEffectView = view as? UIVisualEffectView {
visualEffectView.effect = UIBlurEffect(style: .dark)
} else if let label = view as? UILabel, label.isContainedByViewOfType(UICollectionView.self) {
label.textColor = UIColor.white
} else if let button = view as? UIButton, !button.isContainedByViewOfType(UICollectionView.self) {
button.backgroundColor = UIColor(white: 0.12, alpha: 1.0)
} else if view.bounds.height == 1.0 {
// Color the separator (damn these hacks are getting gross)
view.isOpaque = false
view.backgroundColor = UIColor.white.withAlphaComponent(0.035)
}
view.subviews.forEach { colorSubviewsOfView($0) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment