Skip to content

Commit

Permalink
Merge pull request #282 from cryptomator/bugfix/fix_ios14_swiftui_hos…
Browse files Browse the repository at this point in the history
…tingviewcontroller

Bugfix: Fix iOS 14 SwiftUI HostingViewController missing Navigation Items
  • Loading branch information
tobihagemann authored Dec 6, 2022
2 parents 3264582 + dffcf5c commit 6e4fc46
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
15 changes: 13 additions & 2 deletions Cryptomator/S3/S3AuthenticationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import CryptomatorCommonCore
import SwiftUI
import UIKit

class S3AuthenticationViewController: UIHostingController<S3AuthenticationView> {
class S3AuthenticationViewController: UIViewController {
weak var coordinator: (Coordinator & S3Authenticating)?
let viewModel: S3AuthenticationViewModel
private var subscriptions = Set<AnyCancellable>()

init(viewModel: S3AuthenticationViewModel) {
self.viewModel = viewModel
super.init(rootView: S3AuthenticationView(viewModel: viewModel))
super.init(nibName: nil, bundle: nil)
}

@available(*, unavailable)
Expand All @@ -28,6 +28,8 @@ class S3AuthenticationViewController: UIHostingController<S3AuthenticationView>

override func viewDidLoad() {
super.viewDidLoad()
setupSwiftUIView()

let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
navigationItem.rightBarButtonItem = doneButton
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel))
Expand Down Expand Up @@ -64,4 +66,13 @@ class S3AuthenticationViewController: UIHostingController<S3AuthenticationView>
@objc func cancel() {
coordinator?.cancel()
}

private func setupSwiftUIView() {
let child = UIHostingController(rootView: S3AuthenticationView(viewModel: viewModel))
addChild(child)
view.addSubview(child.view)
child.didMove(toParent: self)
child.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(child.view.constraints(equalTo: view))
}
}
2 changes: 1 addition & 1 deletion Cryptomator/WebDAV/WebDAVAuthentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct WebDAVAuthentication: View {
@FocusStateLegacy private var focusedField: Fields? = .url

var body: some View {
List {
Form {
TextField(LocalizedString.getValue("common.cells.url"), text: $viewModel.url)
.keyboardType(.URL)
.disableAutocorrection(true)
Expand Down
17 changes: 14 additions & 3 deletions Cryptomator/WebDAV/WebDAVAuthenticationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import Promises
import SwiftUI
import UIKit

class WebDAVAuthenticationViewController: UIHostingController<WebDAVAuthentication> {
class WebDAVAuthenticationViewController: UIViewController {
weak var coordinator: (Coordinator & WebDAVAuthenticating)?
private var viewModel: WebDAVAuthenticationViewModel
private let viewModel: WebDAVAuthenticationViewModel
private var cancellables = Set<AnyCancellable>()
private var hud: ProgressHUD?

init(viewModel: WebDAVAuthenticationViewModel) {
self.viewModel = viewModel
super.init(rootView: WebDAVAuthentication(viewModel: viewModel))
super.init(nibName: nil, bundle: nil)
}

@available(*, unavailable)
Expand All @@ -31,6 +31,8 @@ class WebDAVAuthenticationViewController: UIHostingController<WebDAVAuthenticati

override func viewDidLoad() {
super.viewDidLoad()
setupSwiftUIView()

title = "WebDAV"
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel))
navigationItem.leftBarButtonItem = cancelButton
Expand Down Expand Up @@ -87,6 +89,15 @@ class WebDAVAuthenticationViewController: UIHostingController<WebDAVAuthenticati
@objc func cancel() {
coordinator?.cancel()
}

private func setupSwiftUIView() {
let child = UIHostingController(rootView: WebDAVAuthentication(viewModel: viewModel))
addChild(child)
view.addSubview(child.view)
child.didMove(toParent: self)
child.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(child.view.constraints(equalTo: view))
}
}

#if DEBUG
Expand Down

0 comments on commit 6e4fc46

Please sign in to comment.