omnivore/apple/OmnivoreKit/Sources/Utils/UIViewControllerExtensions.swift

75 lines
2.3 KiB
Swift
Raw Permalink Normal View History

2022-02-11 17:24:33 +00:00
#if os(iOS)
import UIKit
public extension UIViewController {
// swiftlint:disable line_length
2022-02-11 17:24:33 +00:00
func embed(childViewController child: UIViewController, heightRatio: CGFloat? = nil) {
addChild(child)
child.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(child.view)
var constraints = [
child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
]
if let heightRatio = heightRatio {
2023-01-23 02:19:22 +00:00
let constraint = child.view.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: heightRatio)
constraints.append(constraint)
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: OperationQueue.main) { _ in
UIView.animate(withDuration: 0.2) {
if let parent = self.parent, let frame = constraint.firstItem?.frame {
constraint.constant = parent.view.frame.height - frame.height - 10
}
child.view.setNeedsLayout()
child.view.layoutIfNeeded()
}
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: OperationQueue.main) { _ in
UIView.animate(withDuration: 0.2) {
constraint.constant = 0
child.view.setNeedsLayout()
child.view.layoutIfNeeded()
}
}
2022-02-11 17:24:33 +00:00
} else {
constraints.append(child.view.topAnchor.constraint(equalTo: view.topAnchor))
}
NSLayoutConstraint.activate(constraints)
child.didMove(toParent: self)
}
}
2024-03-21 02:17:50 +00:00
2022-02-11 17:24:33 +00:00
#endif
#if os(macOS)
import AppKit
public extension NSViewController {
func embed(childViewController child: NSViewController) {
addChild(child)
child.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(child.view)
NSLayoutConstraint.activate([
child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
child.view.topAnchor.constraint(equalTo: view.topAnchor),
child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}
#endif