No cancel button in UIFontPickerViewController

I have made a font picker but the problem is that I can’t seem to figure out why there is no navigation bar with a cancel button and search bar. Normally there would be but with this there isn’t (there is a fontPickerViewControllerDidCancel method but no cancel button).

Has anyone else had this problem, and is there a way to resolve this?

Code Block Swift
struct FontPicker: UIViewControllerRepresentable {
class Coordinator: NSObject, UIFontPickerViewControllerDelegate {
private let parent: FontPicker
init(_ parent: FontPicker) {
self.parent = parent
}
func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
parent.presentationMode.wrappedValue.dismiss()
guard let descriptor = viewController.selectedFontDescriptor else { return }
let font = UIFont(descriptor: descriptor, size: 17)
parent.fontName = font.fontName
}
}
@Environment(\.presentationMode) private var presentationMode
@Binding var fontName: String
func makeUIViewController(context: Context) -> some UIViewController {
let configuration = UIFontPickerViewController.Configuration()
configuration.includeFaces = true
let picker = UIFontPickerViewController(configuration: configuration)
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
}
// used in a View like this
Button("Choose font") { showingFontPicker = true }
.sheet(isPresented: $showingFontPicker) {
FontPicker(fontName: $newFontName)
.ignoresSafeArea()
}

You might need to wrap your FontPicker in a nav view

Code Block
        NavigationView {
            FontPicker(fontName: $newFontName)
        }
        .navigationViewStyle(StackNavigationViewStyle())




Trying this breaks the picker but thanks for the suggestion.
When using a PHPickerViewController in the same way, the navigation bar shows with a search bar and cancel button but with this UIFontPickerViewController it doesn’t.
I have fixed this problem but it doesn't fully work.
The cancel button and search bar now show but the Binding doesn't work now.
How can you pass in and receive data that will change from a UIViewControllerRepresentable to a UIViewController?

Code Block Swift
class FontPickerViewController: UIViewController, UIFontPickerViewControllerDelegate {
var fontName: String = "Helvetica"
var fontPicker = UIFontPickerViewController()
override func viewDidLoad() {
super.viewDidLoad()
fontPicker.delegate = self
let button = UIButton(type: .system)
button.titleLabel?.font = .systemFont(ofSize: 17)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
button.setTitle("Choose font", for: .normal)
button.addTarget(self, action: #selector(showFontPicker(sender:)), for: .touchUpInside)
view.addSubview(button)
}
func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
dismiss(animated: true)
guard let descriptor = viewController.selectedFontDescriptor else { return }
let font = UIFont(descriptor: descriptor, size: 17)
fontName = font.fontName
}
@objc func showFontPicker(sender: UIButton!) {
let configuration = UIFontPickerViewController.Configuration()
configuration.includeFaces = true
fontPicker = UIFontPickerViewController(configuration: configuration)
present(fontPicker, animated: true)
}
}
struct FontPicker: UIViewControllerRepresentable {
@Binding var fontName: String
func makeUIViewController(context: Context) -> some UIViewController {
let picker = FontPickerViewController()
picker.fontName = fontName
return picker
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}
// and then showing this in a view like this:
Form {
FontPicker(fontName: $fontName)
.ignoresSafeArea()
}

No cancel button in UIFontPickerViewController
 
 
Q