Skip to content

Commit

Permalink
REPLAY-1510 Support for image background in system bars
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejburda committed Apr 10, 2023
1 parent e67c2c8 commit 6310d8f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@
<connections>
<outlet property="customButton" destination="KxN-zV-mGU" id="zX2-X5-J8J"/>
<outlet property="customImageView" destination="vKz-Ut-7bS" id="aJg-Sy-OrB"/>
<outlet property="navigationBar" destination="ftc-4B-7Pp" id="Cz6-ki-X2T"/>
<outlet property="tabBar" destination="raw-wh-zEy" id="TaQ-44-tXa"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Ief-a0-LHa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ import UIKit
internal class ImagesViewController: UIViewController {
@IBOutlet weak var customButton: UIButton!
@IBOutlet weak var customImageView: UIImageView!
@IBOutlet weak var tabBar: UITabBar!
@IBOutlet weak var navigationBar: UINavigationBar!

override func viewDidLoad() {
super.viewDidLoad()

customButton.setBackgroundImage(UIImage(color: .lightGray), for: .normal)
let color = UIColor(white: 0, alpha: 0.05)
customButton.setBackgroundImage(UIImage(color: color), for: .normal)

tabBar.backgroundImage = UIImage(color: color)
tabBar.selectedItem = tabBar.items?.first
navigationBar.setBackgroundImage(UIImage(color: color), for: .default)

customImageView.image = UIImage(named: "dd_logo")?.withRenderingMode(.alwaysTemplate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,24 @@ internal struct UIImageViewRecorder: NodeRecorder {
private let shouldRecordImagePredicate: (UIImageView) -> Bool
/// An option for overriding default semantics from parent recorder.
var semanticsOverride: (UIImageView, ViewAttributes) -> NodeSemantics? = { imageView, _ in
let className = "\(type(of: imageView))"
// This gets effective on iOS 15.0+ which is the earliest version that displays some elements in popover views.
// Here we explicitly ignore the "shadow" effect applied to popover.
let isSystemShadow = className == "_UICutoutShadowView"
return isSystemShadow ? IgnoredElement(subtreeStrategy: .ignore) : nil
return imageView.isSystemShadow ? IgnoredElement(subtreeStrategy: .ignore) : nil
}

internal init(
tintColorProvider: @escaping (UIImageView) -> UIColor? = { imageView in
if #available(iOS 13.0, *), let image = imageView.image {
return image.isSymbolImage || image.isAlwaysTemplate ? imageView.tintColor : nil
return image.isTinted ? imageView.tintColor : nil
} else {
return nil
}
},
shouldRecordImagePredicate: @escaping (UIImageView) -> Bool = { imageView in
if #available(iOS 13.0, *), let image = imageView.image {
if let button = imageView.superview as? UIButton, button.buttonType == .custom {
return true
}
return image.isSymbolImage || image.isBundled || image.isAlwaysTemplate
return image.isContextual || imageView.isSystemControlBackground
} else {
return false
}

}
) {
self.tintColorProvider = tintColorProvider
Expand Down Expand Up @@ -162,11 +156,48 @@ internal struct UIImageViewWireframesBuilder: NodeWireframesBuilder {
}

fileprivate extension UIImage {
var isBundled: Bool {
@available(iOS 13.0, *)
var isContextual: Bool {
return isSymbolImage || isBundled || isAlwaysTemplate
}

@available(iOS 13.0, *)
var isTinted: Bool {
return isSymbolImage || isAlwaysTemplate
}

private var isBundled: Bool {
return description.contains("named(")
}

var isAlwaysTemplate: Bool {
private var isAlwaysTemplate: Bool {
return renderingMode == .alwaysTemplate
}
}

fileprivate extension UIImageView {

var isSystemControlBackground: Bool {
return isButtonBackground || isBarBackground
}

var isSystemShadow: Bool {
let className = "\(type(of: self))"
// This gets effective on iOS 15.0+ which is the earliest version that displays some elements in popover views.
// Here we explicitly ignore the "shadow" effect applied to popover.
return className == "_UICutoutShadowView"
}

var isButtonBackground: Bool {
if let button = superview as? UIButton, button.buttonType == .custom {
return button.backgroundImage(for: button.state) == image
}
return false
}

var isBarBackground: Bool {
guard let superview = superview else { return false }
let superViewType = "\(type(of: superview))"
return superViewType == "_UIBarBackground"
}
}

0 comments on commit 6310d8f

Please sign in to comment.