-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathExtensions.swift
More file actions
74 lines (62 loc) · 2.33 KB
/
Copy pathExtensions.swift
File metadata and controls
74 lines (62 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Foundation
import SwiftUI
import UIKit
/**
Helper used to render UIView inside of SwiftUI.
*/
struct RepresentableView: UIViewRepresentable {
var view: UIView
// Adding a wrapper UIView to avoid SwiftUI directly managing React Native views.
// This fixes issues with incorrect layout rendering.
func makeUIView(context: Context) -> UIView {
let wrapper = UIView()
wrapper.addSubview(view)
return wrapper
}
func updateUIView(_ uiView: UIView, context: Context) {}
}
extension Collection {
// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}
extension UIView {
func pinEdges(to other: UIView) {
NSLayoutConstraint.activate([
leadingAnchor.constraint(equalTo: other.leadingAnchor),
trailingAnchor.constraint(equalTo: other.trailingAnchor),
topAnchor.constraint(equalTo: other.topAnchor),
bottomAnchor.constraint(equalTo: other.bottomAnchor)
])
}
}
extension UIHostingController {
convenience public init(rootView: Content, ignoreSafeArea: Bool) {
self.init(rootView: rootView)
if ignoreSafeArea {
disableSafeArea()
}
}
/// Disables safe area insets by dynamically subclassing the hosting controller's view
/// and overriding safeAreaInsets to return .zero.
func disableSafeArea() {
guard let viewClass = object_getClass(view) else { return }
let viewSubclassName = String(cString: class_getName(viewClass)).appending("_IgnoreSafeArea")
if let viewSubclass = NSClassFromString(viewSubclassName) {
object_setClass(view, viewSubclass)
}
else {
guard let viewClassNameUtf8 = (viewSubclassName as NSString).utf8String else { return }
guard let viewSubclass = objc_allocateClassPair(viewClass, viewClassNameUtf8, 0) else { return }
if let method = class_getInstanceMethod(UIView.self, #selector(getter: UIView.safeAreaInsets)) {
let safeAreaInsets: @convention(block) (AnyObject) -> UIEdgeInsets = { _ in
return .zero
}
class_addMethod(viewSubclass, #selector(getter: UIView.safeAreaInsets), imp_implementationWithBlock(safeAreaInsets), method_getTypeEncoding(method))
}
objc_registerClassPair(viewSubclass)
object_setClass(view, viewSubclass)
}
}
}