Set property on ViewController in UIViewControllerRepresentable - swift

How can I change a property on a UIViewController presented via. a UIViewControllerRepresentable ?
Sample code of how I would expect it to work, however it doesn't. How can I make it work?
(color is just a example, please don't focus on that)
class MyViewController: UIViewController {
var color: UIColor? = nil {
didSet {
guard isViewLoaded else { return }
view.layer.backgroundColor = color?.cgColor
}
}
override func viewDidLoad() {
view.layer.backgroundColor = color?.cgColor
}
}
struct MyView: UIViewControllerRepresentable {
#State private var color: UIColor?
func makeUIViewController(context: UIViewControllerRepresentableContext<MyView>) -> MyViewController {
let viewController = MyViewController()
viewController.color = color // always nil?
return viewController
}
func updateUIViewController(_ uiViewController: MyViewController,
context: UIViewControllerRepresentableContext<MyView>) {
uiViewController.color = color // always nil?
}
}
extension MyView {
func color(_ color: UIColor) -> MyView {
self.color = color // does nothing?
return self
}
}
struct ContentView: View {
var body: some View {
MyView()
.color(.magenta)
}
}

Here is possible approach (if you expect that color can be modified externally, as it is seen). Tested with Xcode 11.4 / iOS 13.4
struct MyView: UIViewControllerRepresentable {
#Binding var color: UIColor?
func makeUIViewController(context: UIViewControllerRepresentableContext<MyView>) -> MyViewController {
let viewController = MyViewController()
viewController.color = color // always nil?
return viewController
}
func updateUIViewController(_ uiViewController: MyViewController,
context: UIViewControllerRepresentableContext<MyView>) {
uiViewController.color = color // always nil?
}
}
struct ContentView: View {
#State private var color: UIColor? = .magenta
var body: some View {
MyView(color: $color)
// MyView(color: .constant(.magenta)) // alternate usage
}
}

Another solution based on idea from Asperi's answer:
struct MyView: UIViewControllerRepresentable {
private class State: ObservableObject {
var color: UIColor?
}
#Binding private var state: State
init() {
_state = .constant(State())
}
func makeUIViewController(context: UIViewControllerRepresentableContext<MyView>) -> MyViewController {
return MyViewController()
}
func updateUIViewController(_ uiViewController: MyViewController,
context: UIViewControllerRepresentableContext<MyView>) {
uiViewController.color = state.color
}
}
extension MyView {
func color(_ color: UIColor) -> MyView {
self.state.color = color
return self
}
}
Or a even simpler version, we just use #Binding for the wrapped ViewController directly
struct MyView: UIViewControllerRepresentable {
#Binding private var viewController: MyViewController
init() {
_viewController = .constant(MyViewController())
}
func makeUIViewController(context: UIViewControllerRepresentableContext<MyView>) -> MyViewController {
return viewController
}
func updateUIViewController(_ uiViewController: MyViewController,
context: UIViewControllerRepresentableContext<MyView>) {
}
}
extension MyView {
func color(_ color: UIColor) -> MyView {
self.viewController.color = color
return self
}
}

Related

Lock device orientation for SwiftUI UIViewControllerRepresentable

I have a UIViewController that I need to wrap and use as a SwiftUI view. I have implemented the methods needed to lock device orientation for that view controller. When I use the UIViewController in an UIKit context, the orientation could be locked.
class ContentViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.red
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
override var shouldAutorotate: Bool {
return false
}
}
When I wrap it as a SwiftUI view with UIViewControllerRepresentable, the orientation could not be locked. The
struct ContentWrappedView : UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> ContentViewController {
return ContentViewController.init()
}
func updateUIViewController(_ uiViewController: ContentViewController, context: Context) {
}
}
Is this expected behavior? Is there a way around it?
I'll be using it in a framework and so, implementing it with AppDelegate doesn't work for me. Thanks in advance.

Connecting UIKit with SwiftUI, why did I lose my NavBar?

So when I use the old UIKit Lifecycle with SceneDelegate and AppDelegate I get the desired result. But when I use the new App life cycle I can't figure out to get the NavBar back..
import SwiftUI
struct ContentView: View {
var body: some View {
DiffableContainer()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
DiffableContainer()
.ignoresSafeArea()
}
}
struct DiffableContainer: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
return UINavigationController(rootViewController: DiffableTableVC())
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
class DiffableTableVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = .green
navigationItem.title = "Test"
title = "Hello"
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print("This is set up and running")
return true
}
}
Here is the lifecycle that I am using now:
#main
struct ContactsApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

QLPreviewController missing navigation bar in SwiftUI

QLPreviewController navigation bar missing when I presented in sheet.
It looks like this :
How can I show up top navigation bar or navigationItem?
QuickLookController UIKit representable in swiftUI
.sheet(isPresented: $showQuickLook, onDismiss: {self.showQuickLook = false}) {
if self.selectedURL != nil {
QuickLookController(url: self.selectedURL!) {
self.showQuickLook = false
}.edgesIgnoringSafeArea(.all)
}
}
struct QuickLookController: UIViewControllerRepresentable {
var url: URL
var onDismiss: () -> Void
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIViewController(_ uiViewController: QLPreviewController, context: UIViewControllerRepresentableContext<QuickLookController>) {
uiViewController.reloadData()
}
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
controller.reloadData()
return controller
}
class Coordinator: NSObject, QLPreviewControllerDataSource {
var parent: QuickLookController
init(_ qlPreviewController: QuickLookController) {
self.parent = qlPreviewController
super.init()
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return self.parent.url as QLPreviewItem
}
}
}
QuickLook preview does not have own navigation controller, it is our responsibility to provided one.
Here is possible approach. Tested with Xcode 11.4 / iOS 13.4
struct TestQLPreviewController: View {
#State private var showQuickLook = false
// just for demo - document.pdf is located in main bundle
#State private var selectedURL = Bundle.main.url(forResource: "document", withExtension: "pdf")
var body: some View {
Button("Show") { self.showQuickLook.toggle() }
.sheet(isPresented: $showQuickLook, onDismiss: {self.showQuickLook = false}) {
if self.selectedURL != nil {
QuickLookController(url: self.selectedURL!) {
self.showQuickLook = false
}
}
}
}
}
struct QuickLookController: UIViewControllerRepresentable {
var url: URL
var onDismiss: () -> Void
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIViewController(_ viewController: UINavigationController, context: UIViewControllerRepresentableContext<QuickLookController>) {
if let controller = viewController.topViewController as? QLPreviewController {
controller.reloadData()
}
}
func makeUIViewController(context: Context) -> UINavigationController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
controller.reloadData()
return UINavigationController(rootViewController: controller)
}
class Coordinator: NSObject, QLPreviewControllerDataSource {
var parent: QuickLookController
init(_ qlPreviewController: QuickLookController) {
self.parent = qlPreviewController
super.init()
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return self.parent.url as QLPreviewItem
}
}
}
Here is an implementation that can handle multiple urls:
import SwiftUI
import QuickLook
struct QuickLookView: UIViewControllerRepresentable {
var urls: [URL]
func updateUIViewController(_ uiViewController: QLPreviewController,
context: UIViewControllerRepresentableContext<QuickLookView>) {
uiViewController.reloadData()
}
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
controller.reloadData()
return controller
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, QLPreviewControllerDataSource {
var parent: QuickLookView
init(_ qlPreviewController: QuickLookView) {
self.parent = qlPreviewController
super.init()
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return parent.urls.count
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return parent.urls[index] as QLPreviewItem
}
}
}
Then wrap the QuickLookView in a container and access that from your views:
struct QuickLookContainerView: View {
let urls: [URL]
var body: some View {
NavigationView {
QuickLookView(urls: urls)
}
}
}

Is possible to combine together in same View SwiftUIView and UIViewControllerRepresentable?

I am wondering if it's possible to combine a View and a UIViewControllerRepresentable in a same view.
I tried:
//Here I declare MyViewController:
class MyViewController: UIViewController {
override viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}
struct MyViewControllerIntegrate: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MyViewControllerIntegrate>) -> MyViewController {
return MyViewController()
}
func updateUIViewController(_ uiViewController: MyViewController, context: UIViewControllerRepresentableContext<MyViewControllerIntegrate>) {
}
}
struct MyView: View {
var body: some View {
Text("Hello StackOverflow!")
}
}
struct ContentView: View {
var body: some View {
MyView()
MyViewController()
.frame(height: 400)
}
}
Xcode shows me an error message:
Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
Any hints? Thank you
Put them in Group (or in some stack, eg. VStack)
struct ContentView: View {
var body: some View {
Group {
MyView()
MyViewController()
.frame(height: 400)
}
}
}

Does somebody have all ready implemented searchbar on tvos with swiftui?

Does somebody have all ready implemented a search bar using Apple component like UISearchBar with swiftui on tvos ?
I tried this UISearchBar(frame: .zero) but I got this error init(frame:)' is unavailable in tvOS
I only found solutions for ios.
The scheme of initial setup should be like below. Of course, the logic of searching/filtering/showing results is app specific.
import SwiftUI
import TVUIKit
struct SearchView: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<SearchView>) -> UINavigationController {
let controller = UISearchController(searchResultsController: context.coordinator)
controller.searchResultsUpdater = context.coordinator
return UINavigationController(rootViewController: UISearchContainerViewController(searchController: controller))
}
func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<SearchView>) {
}
func makeCoordinator() -> SearchView.Coordinator {
Coordinator()
}
typealias UIViewControllerType = UINavigationController
class Coordinator: UIViewController, UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
// do here what's needed
}
}
}
struct ContentView: View {
#State private var text: String = ""
var body: some View {
VStack {
SearchView()
Spacer()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}