session-ios/SignalMessaging/Views/OWSNavigationBar.swift

244 lines
8 KiB
Swift
Raw Normal View History

//
2019-01-16 15:55:23 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
import UIKit
@objc
2018-05-25 21:15:19 +02:00
public protocol NavBarLayoutDelegate: class {
func navBarCallLayoutDidChange(navbar: OWSNavigationBar)
}
@objc
2018-05-25 21:15:19 +02:00
public class OWSNavigationBar: UINavigationBar {
2018-05-25 21:15:19 +02:00
@objc
public weak var navBarLayoutDelegate: NavBarLayoutDelegate?
2018-05-25 21:15:19 +02:00
@objc
public let navbarWithoutStatusHeight: CGFloat = 44
2018-05-25 21:15:19 +02:00
@objc
public var callBannerHeight: CGFloat {
2018-07-17 21:40:54 +02:00
return OWSWindowManagerCallBannerHeight()
}
2018-05-25 21:15:19 +02:00
@objc
public var statusBarHeight: CGFloat {
return CurrentAppContext().statusBarHeight
2018-05-17 20:52:46 +02:00
}
2018-05-25 21:15:19 +02:00
@objc
public var fullWidth: CGFloat {
2018-05-17 23:58:43 +02:00
return UIScreen.main.bounds.size.width
}
2018-05-25 21:15:19 +02:00
public required init?(coder aDecoder: NSCoder) {
2018-08-27 16:21:03 +02:00
notImplemented()
}
@objc
public static let backgroundBlurMutingFactor: CGFloat = 0.5
var blurEffectView: UIVisualEffectView?
override init(frame: CGRect) {
super.init(frame: frame)
applyTheme()
NotificationCenter.default.addObserver(self, selector: #selector(callDidChange), name: .OWSWindowManagerCallDidChange, object: nil)
2019-03-30 14:22:31 +01:00
NotificationCenter.default.addObserver(self, selector: #selector(didChangeStatusBarFrame), name: UIApplication.didChangeStatusBarFrameNotification, object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(themeDidChange),
name: .ThemeDidChange,
object: nil)
}
// MARK: FirstResponder Stubbing
@objc
public weak var stubbedNextResponder: UIResponder?
override public var next: UIResponder? {
if let stubbedNextResponder = self.stubbedNextResponder {
return stubbedNextResponder
}
return super.next
}
// MARK: Theme
private func applyTheme() {
2018-11-14 17:09:24 +01:00
guard respectsTheme else {
return
}
2020-02-17 02:08:46 +01:00
backgroundColor = Colors.navigationBarBackground
2020-01-21 01:30:01 +01:00
2020-02-17 02:08:46 +01:00
tintColor = Colors.text
2020-01-20 03:20:27 +01:00
2019-03-30 14:22:31 +01:00
if UIAccessibility.isReduceTransparencyEnabled {
blurEffectView?.isHidden = true
let color = Theme.navbarBackgroundColor
let backgroundImage = UIImage(color: color)
self.setBackgroundImage(backgroundImage, for: .default)
} else {
// Make navbar more translucent than default. Navbars remove alpha from any assigned backgroundColor, so
// to achieve transparency, we have to assign a transparent image.
2019-06-14 07:25:39 +02:00
let color = Theme.navbarBackgroundColor
let backgroundImage = UIImage(color: color)
self.setBackgroundImage(backgroundImage, for: .default)
2020-01-20 03:20:27 +01:00
/*
let blurEffect = Theme.barBlurEffect
let blurEffectView: UIVisualEffectView = {
if let existingBlurEffectView = self.blurEffectView {
2018-11-22 05:12:40 +01:00
existingBlurEffectView.isHidden = false
return existingBlurEffectView
}
let blurEffectView = UIVisualEffectView()
blurEffectView.isUserInteractionEnabled = false
self.blurEffectView = blurEffectView
self.insertSubview(blurEffectView, at: 0)
// navbar frame doesn't account for statusBar, so, same as the built-in navbar background, we need to exceed
// the navbar bounds to have the blur extend up and behind the status bar.
blurEffectView.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(top: -statusBarHeight, left: 0, bottom: 0, right: 0))
return blurEffectView
}()
blurEffectView.effect = blurEffect
2020-01-20 03:20:27 +01:00
*/
// remove hairline below bar.
self.shadowImage = UIImage()
// On iOS11, despite inserting the blur at 0, other views are later inserted into the navbar behind the blur,
// so we have to set a zindex to avoid obscuring navbar title/buttons.
2020-01-20 03:20:27 +01:00
// blurEffectView.layer.zPosition = -1
}
}
2018-08-08 17:31:45 +02:00
@objc
public func themeDidChange() {
2018-08-23 16:37:34 +02:00
Logger.debug("")
applyTheme()
2018-08-08 17:31:45 +02:00
}
2018-11-14 17:09:24 +01:00
@objc
public var respectsTheme: Bool = true {
didSet {
themeDidChange()
}
}
// MARK: Layout
2018-05-17 20:52:46 +02:00
@objc
public func callDidChange() {
2018-08-23 16:37:34 +02:00
Logger.debug("")
self.navBarLayoutDelegate?.navBarCallLayoutDidChange(navbar: self)
}
@objc
public func didChangeStatusBarFrame() {
2018-08-23 16:37:34 +02:00
Logger.debug("")
self.navBarLayoutDelegate?.navBarCallLayoutDidChange(navbar: self)
}
2018-05-25 21:15:19 +02:00
public override func sizeThatFits(_ size: CGSize) -> CGSize {
2018-05-17 20:52:46 +02:00
guard OWSWindowManager.shared().hasCall() else {
return super.sizeThatFits(size)
}
2018-05-17 20:52:46 +02:00
if #available(iOS 11, *) {
return super.sizeThatFits(size)
2018-07-31 22:28:41 +02:00
} else if #available(iOS 10, *) {
// iOS10
// sizeThatFits is repeatedly called to determine how much space to reserve for that navbar.
// That is, increasing this causes the child view controller to be pushed down.
// (as of iOS11, this is not used and instead we use additionalSafeAreaInsets)
return CGSize(width: fullWidth, height: navbarWithoutStatusHeight + statusBarHeight)
2018-07-31 22:28:41 +02:00
} else {
// iOS9
// sizeThatFits is repeatedly called to determine how much space to reserve for that navbar.
// That is, increasing this causes the child view controller to be pushed down.
// (as of iOS11, this is not used and instead we use additionalSafeAreaInsets)
return CGSize(width: fullWidth, height: navbarWithoutStatusHeight + callBannerHeight + 20)
2018-05-17 20:52:46 +02:00
}
}
2018-05-25 21:15:19 +02:00
public override func layoutSubviews() {
2019-01-16 15:55:23 +01:00
guard CurrentAppContext().isMainApp else {
super.layoutSubviews()
return
}
guard OWSWindowManager.shared().hasCall() else {
super.layoutSubviews()
return
}
2018-07-31 22:28:41 +02:00
guard #available(iOS 11, *) else {
super.layoutSubviews()
return
}
2018-05-17 23:58:43 +02:00
self.frame = CGRect(x: 0, y: callBannerHeight, width: fullWidth, height: navbarWithoutStatusHeight)
self.bounds = CGRect(x: 0, y: 0, width: fullWidth, height: navbarWithoutStatusHeight)
super.layoutSubviews()
2018-05-17 22:12:51 +02:00
// This is only necessary on iOS11, which has some private views within that lay outside of the navbar.
// They aren't actually visible behind the call status bar, but they looks strange during present/dismiss
// animations for modal VC's
for subview in self.subviews {
let stringFromClass = NSStringFromClass(subview.classForCoder)
if stringFromClass.contains("BarBackground") {
2018-05-17 22:12:51 +02:00
subview.frame = self.bounds
} else if stringFromClass.contains("BarContentView") {
2018-05-17 22:12:51 +02:00
subview.frame = self.bounds
}
}
}
// MARK: Override Theme
@objc
public enum NavigationBarThemeOverride: Int {
case clear, alwaysDark
2018-11-22 05:12:40 +01:00
}
@objc
public func overrideTheme(type: NavigationBarThemeOverride) {
respectsTheme = false
barStyle = .black
2019-03-30 14:22:31 +01:00
titleTextAttributes = [NSAttributedString.Key.foregroundColor: Theme.darkThemePrimaryColor]
barTintColor = Theme.darkThemeBackgroundColor.withAlphaComponent(0.6)
tintColor = Theme.darkThemePrimaryColor
switch type {
case .clear:
blurEffectView?.isHidden = true
clipsToBounds = true
// Making a toolbar transparent requires setting an empty uiimage
setBackgroundImage(UIImage(), for: .default)
shadowImage = UIImage()
backgroundColor = .clear
case .alwaysDark:
blurEffectView?.isHidden = false
clipsToBounds = false
setBackgroundImage(nil, for: .default)
shadowImage = nil
2018-11-22 05:12:40 +01:00
}
}
}