Swift IOS - Using UIPickerView AND Keyboard -
in app when user clicks on uitextfield should able pick value uipickerview or enter own value using keyboard.
what's best way of doing in terms of user experience , ease of implementation?
uipickerview toolbar implemented.
i'd appreciate both advice on best way of doing , example code of switching keyboard <-> pickerview.
i've tried adding button "show keyboard" on pickerview's toolbar , adding following code:
func showkeyboard() { selectedtextfield.inputview = nil }
but clicking button doesn't anything. i'm not sure it's way in terms of ux.
here's solution:
var usekeyboard:bool = true func showkeyboard() { if usekeyboard { usekeyboard = false selectedtextfield.inputview = nil selectedtextfield.reloadinputviews() selectedtextfield.keyboardappearance = uikeyboardappearance.default selectedtextfield.keyboardtype = uikeyboardtype.default } else { usekeyboard = true selectedtextfield.inputview = nil selectedtextfield.reloadinputviews() createpicker(selectedtextfield) selectedtextfield.resignfirstresponder() selectedtextfield.becomefirstresponder() } } // that's custom picker - adjust whatever need func createpicker(sender: uitextfield){ selectedtextfield = sender // create picker view var newpickerview: uipickerview newpickerview = uipickerview(frame: cgrectmake(0, 200, view.frame.width, 300)) newpickerview.backgroundcolor = .whitecolor() // uipickerview newpickerview.showsselectionindicator = true newpickerview.delegate = self newpickerview.datasource = self // create toolbar var toolbar = uitoolbar() toolbar.barstyle = uibarstyle.default toolbar.translucent = true toolbar.tintcolor = uicolor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) toolbar.sizetofit() // create buttons var donebutton = uibarbuttonitem(title: "done", style: uibarbuttonitemstyle.plain, target: self, action: "donepicker") var spacebutton = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.flexiblespace, target: nil, action: nil) var cancelbutton = uibarbuttonitem(title: "cancel", style: uibarbuttonitemstyle.plain, target: self, action: "cancelpicker") var custombutton = uibarbuttonitem(title: "keyboard", style: uibarbuttonitemstyle.plain, target: self, action: "showkeyboard") // assign buttons toolbar toolbar.setitems([cancelbutton, spacebutton, custombutton, donebutton], animated: false) toolbar.userinteractionenabled = true // add pickerview , toolbar textfield sender.inputview = newpickerview sender.inputaccessoryview = toolbar } func donepicker() { usekeyboard = true selectedtextfield.resignfirstresponder() } func cancelpicker() { usekeyboard = true selectedtextfield.resignfirstresponder() }
Comments
Post a Comment