Files
SideStore/SideStoreApp/Sources/SideStoreUIKit/Components/NavigationBar.swift

89 lines
2.8 KiB
Swift
Raw Normal View History

//
// NavigationBar.swift
// AltStore
//
// Created by Riley Testut on 7/15/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import UIKit
2023-03-01 14:36:52 -05:00
import RoxasUIKit
@objc
2023-03-01 00:48:36 -05:00
final class NavigationBar: UINavigationBar {
@objc
2019-09-05 15:37:58 -07:00
@IBInspectable var automaticallyAdjustsItemPositions: Bool = true
2023-03-01 00:48:36 -05:00
2019-09-05 11:59:10 -07:00
private let backgroundColorView = UIView()
2023-03-01 00:48:36 -05:00
override init(frame: CGRect) {
super.init(frame: frame)
2023-03-01 00:48:36 -05:00
initialize()
}
2023-03-01 00:48:36 -05:00
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
2023-03-01 00:48:36 -05:00
initialize()
}
2023-03-01 00:48:36 -05:00
private func initialize() {
if #available(iOS 13, *) {
2019-10-24 13:04:30 -07:00
let standardAppearance = UINavigationBarAppearance()
standardAppearance.configureWithDefaultBackground()
standardAppearance.shadowColor = nil
2023-03-01 00:48:36 -05:00
2019-10-24 13:04:30 -07:00
let edgeAppearance = UINavigationBarAppearance()
edgeAppearance.configureWithOpaqueBackground()
edgeAppearance.backgroundColor = self.barTintColor
edgeAppearance.shadowColor = nil
2023-03-01 00:48:36 -05:00
if let tintColor = self.barTintColor {
2019-10-24 13:04:30 -07:00
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
2023-03-01 00:48:36 -05:00
2019-10-24 13:04:30 -07:00
standardAppearance.backgroundColor = tintColor
standardAppearance.titleTextAttributes = textAttributes
standardAppearance.largeTitleTextAttributes = textAttributes
2023-03-01 00:48:36 -05:00
2019-10-24 13:04:30 -07:00
edgeAppearance.titleTextAttributes = textAttributes
edgeAppearance.largeTitleTextAttributes = textAttributes
2023-03-01 00:48:36 -05:00
} else {
2019-10-24 13:04:30 -07:00
standardAppearance.backgroundColor = nil
}
2023-03-01 00:48:36 -05:00
2019-10-24 13:04:30 -07:00
self.scrollEdgeAppearance = edgeAppearance
self.standardAppearance = standardAppearance
2023-03-01 00:48:36 -05:00
} else {
shadowImage = UIImage()
if let tintColor = barTintColor {
backgroundColorView.backgroundColor = tintColor
2019-10-24 13:04:30 -07:00
// Top = -50 to cover status bar area above navigation bar on any device.
// Bottom = -1 to prevent a flickering gray line from appearing.
2023-03-01 00:48:36 -05:00
addSubview(backgroundColorView, pinningEdgesWith: UIEdgeInsets(top: -50, left: 0, bottom: -1, right: 0))
} else {
barTintColor = .white
2019-10-24 13:04:30 -07:00
}
2019-09-05 11:59:10 -07:00
}
}
2023-03-01 00:48:36 -05:00
override func layoutSubviews() {
super.layoutSubviews()
2023-03-01 00:48:36 -05:00
if backgroundColorView.superview != nil {
insertSubview(backgroundColorView, at: 1)
2019-09-05 11:59:10 -07:00
}
2023-03-01 00:48:36 -05:00
if automaticallyAdjustsItemPositions {
2019-09-05 15:37:58 -07:00
// We can't easily shift just the back button up, so we shift the entire content view slightly.
2023-03-01 00:48:36 -05:00
for contentView in subviews {
2019-09-05 15:37:58 -07:00
guard NSStringFromClass(type(of: contentView)).contains("ContentView") else { continue }
contentView.center.y -= 2
}
}
}
}