Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is UITextField+UIPickerView supported under Mac Catalyst?

In my iOS app I have a textfield with a picker view assigned to it's inputView. On iOS devices the picker view pops up whenever the user taps the textfield. The same code does not do that, however, when I run it as a Mac/Catalyst app. My debug effort so far shows that the picker view methods are not called at all, so if the Mac requires additional textfield delegate methods to take focus away from the Mac's keyboard? Any idea how to make this work on a Mac? A barebones example code is below.

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var myTextField: UITextField!

var myPickerView : UIPickerView!
var pickerData = ["Alpha" , "Bravo" , "Charlie" , "Delta"]

override func viewDidLoad() {
    super.viewDidLoad()

    self.myPickerView = UIPickerView(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 300))
    self.myPickerView.delegate = self
    self.myPickerView.dataSource = self

    self.myTextField.delegate = self
    self.myTextField.inputView = self.myPickerView

}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
 }
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}
}
like image 539
Electro-Bunny Avatar asked Feb 07 '20 18:02

Electro-Bunny


People also ask

What is UIPickerView component?

UIPickerView is a subclass of UIView . It is useful for iOS apps that use a spinning wheel with many choices for the user. @MainActor class UIPickerView: UIView. One example of this type of app is a slot machine, where a user spins the wheels to play the game.

What is UIPickerView in swift?

A view that uses a spinning-wheel or slot-machine metaphor to show one or more sets of values.

What is pickerview iOS?

A picker view can be defined as the view that shows a spinning wheel or slot machine to display one or more set of values so that the user can spin the wheel to select one. It is an instance of UIPickerView class which inherits UIView.

What is a picker view?

Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year).


1 Answers

It looks like a bug in Mac Catalyst. Try using the UIPicker embedded in an UIActionSheet. ActionSheetPicker-3.0 is a ready-made solution. Show the ActionSheetPicker in textFieldDidBeginEditing of your UITextField.

like image 127
Kevin Avatar answered Sep 21 '22 12:09

Kevin