How to hide keyboard while using UIPickerView with Swift 4?












0















I have got a problem with my code because I am using a UIPickerView but when we switch from one textfield to another, keyboard is not hiding despite my code below.



I would be glad to have someone to help me.



(I have done research about this error but none of code I have seen solved my problem)



@IBOutlet weak var colorPickerView: UIPickerView!

public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.colorTxtField.text! = self.list[row]
self.colorPickerView.isHidden = true
}


func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.colorTxtField {
self.colorPickerView.isHidden = false
textField.endEditing(true)

}
}


I would be glad to have someone to help me.










share|improve this question

























  • How do you display the picker view?

    – rmaddy
    Jan 1 at 16:26











  • @rmaddy from storyboard

    – Auyem
    Jan 1 at 16:27













  • Maybe calling endEditing inside of textFieldDidBeginEditing is not supported / allowed. Have you tried using textFieldShouldBeginEditing instead and return false for self.colorTxtField.

    – D. Mika
    Jan 1 at 17:03













  • @D.Mika I'll try that thank you

    – Auyem
    Jan 1 at 17:12
















0















I have got a problem with my code because I am using a UIPickerView but when we switch from one textfield to another, keyboard is not hiding despite my code below.



I would be glad to have someone to help me.



(I have done research about this error but none of code I have seen solved my problem)



@IBOutlet weak var colorPickerView: UIPickerView!

public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.colorTxtField.text! = self.list[row]
self.colorPickerView.isHidden = true
}


func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.colorTxtField {
self.colorPickerView.isHidden = false
textField.endEditing(true)

}
}


I would be glad to have someone to help me.










share|improve this question

























  • How do you display the picker view?

    – rmaddy
    Jan 1 at 16:26











  • @rmaddy from storyboard

    – Auyem
    Jan 1 at 16:27













  • Maybe calling endEditing inside of textFieldDidBeginEditing is not supported / allowed. Have you tried using textFieldShouldBeginEditing instead and return false for self.colorTxtField.

    – D. Mika
    Jan 1 at 17:03













  • @D.Mika I'll try that thank you

    – Auyem
    Jan 1 at 17:12














0












0








0


0






I have got a problem with my code because I am using a UIPickerView but when we switch from one textfield to another, keyboard is not hiding despite my code below.



I would be glad to have someone to help me.



(I have done research about this error but none of code I have seen solved my problem)



@IBOutlet weak var colorPickerView: UIPickerView!

public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.colorTxtField.text! = self.list[row]
self.colorPickerView.isHidden = true
}


func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.colorTxtField {
self.colorPickerView.isHidden = false
textField.endEditing(true)

}
}


I would be glad to have someone to help me.










share|improve this question
















I have got a problem with my code because I am using a UIPickerView but when we switch from one textfield to another, keyboard is not hiding despite my code below.



I would be glad to have someone to help me.



(I have done research about this error but none of code I have seen solved my problem)



@IBOutlet weak var colorPickerView: UIPickerView!

public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.colorTxtField.text! = self.list[row]
self.colorPickerView.isHidden = true
}


func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.colorTxtField {
self.colorPickerView.isHidden = false
textField.endEditing(true)

}
}


I would be glad to have someone to help me.







ios swift uipickerview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 16:25









rmaddy

243k27320382




243k27320382










asked Jan 1 at 16:18









AuyemAuyem

71




71













  • How do you display the picker view?

    – rmaddy
    Jan 1 at 16:26











  • @rmaddy from storyboard

    – Auyem
    Jan 1 at 16:27













  • Maybe calling endEditing inside of textFieldDidBeginEditing is not supported / allowed. Have you tried using textFieldShouldBeginEditing instead and return false for self.colorTxtField.

    – D. Mika
    Jan 1 at 17:03













  • @D.Mika I'll try that thank you

    – Auyem
    Jan 1 at 17:12



















  • How do you display the picker view?

    – rmaddy
    Jan 1 at 16:26











  • @rmaddy from storyboard

    – Auyem
    Jan 1 at 16:27













  • Maybe calling endEditing inside of textFieldDidBeginEditing is not supported / allowed. Have you tried using textFieldShouldBeginEditing instead and return false for self.colorTxtField.

    – D. Mika
    Jan 1 at 17:03













  • @D.Mika I'll try that thank you

    – Auyem
    Jan 1 at 17:12

















How do you display the picker view?

– rmaddy
Jan 1 at 16:26





How do you display the picker view?

– rmaddy
Jan 1 at 16:26













@rmaddy from storyboard

– Auyem
Jan 1 at 16:27







@rmaddy from storyboard

– Auyem
Jan 1 at 16:27















Maybe calling endEditing inside of textFieldDidBeginEditing is not supported / allowed. Have you tried using textFieldShouldBeginEditing instead and return false for self.colorTxtField.

– D. Mika
Jan 1 at 17:03







Maybe calling endEditing inside of textFieldDidBeginEditing is not supported / allowed. Have you tried using textFieldShouldBeginEditing instead and return false for self.colorTxtField.

– D. Mika
Jan 1 at 17:03















@D.Mika I'll try that thank you

– Auyem
Jan 1 at 17:12





@D.Mika I'll try that thank you

– Auyem
Jan 1 at 17:12












1 Answer
1






active

oldest

votes


















0














I think you are showing pickerView wrongly. You, need not to write




func textFieldDidBeginEditing(_ textField: UITextField)




instead to show picker, just write this line in your viewDidLoad



self.colorTxtField.inputView = self.colorPickerView





share|improve this answer
























  • thank for your response , now I have got this error *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7ff65b720ef0> should have parent view controller:<project1.Add2ViewController: 0x7ff65b5292b0> but requested parent is:<UIInputWindowController: 0x7ff65c04e800>'

    – Auyem
    Jan 1 at 17:36











  • seems like your view hierarchy is not correct. Can you tell me how you are placing this textField and pickerView in your viewController ?

    – nikksindia
    Jan 1 at 17:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997005%2fhow-to-hide-keyboard-while-using-uipickerview-with-swift-4%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think you are showing pickerView wrongly. You, need not to write




func textFieldDidBeginEditing(_ textField: UITextField)




instead to show picker, just write this line in your viewDidLoad



self.colorTxtField.inputView = self.colorPickerView





share|improve this answer
























  • thank for your response , now I have got this error *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7ff65b720ef0> should have parent view controller:<project1.Add2ViewController: 0x7ff65b5292b0> but requested parent is:<UIInputWindowController: 0x7ff65c04e800>'

    – Auyem
    Jan 1 at 17:36











  • seems like your view hierarchy is not correct. Can you tell me how you are placing this textField and pickerView in your viewController ?

    – nikksindia
    Jan 1 at 17:40
















0














I think you are showing pickerView wrongly. You, need not to write




func textFieldDidBeginEditing(_ textField: UITextField)




instead to show picker, just write this line in your viewDidLoad



self.colorTxtField.inputView = self.colorPickerView





share|improve this answer
























  • thank for your response , now I have got this error *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7ff65b720ef0> should have parent view controller:<project1.Add2ViewController: 0x7ff65b5292b0> but requested parent is:<UIInputWindowController: 0x7ff65c04e800>'

    – Auyem
    Jan 1 at 17:36











  • seems like your view hierarchy is not correct. Can you tell me how you are placing this textField and pickerView in your viewController ?

    – nikksindia
    Jan 1 at 17:40














0












0








0







I think you are showing pickerView wrongly. You, need not to write




func textFieldDidBeginEditing(_ textField: UITextField)




instead to show picker, just write this line in your viewDidLoad



self.colorTxtField.inputView = self.colorPickerView





share|improve this answer













I think you are showing pickerView wrongly. You, need not to write




func textFieldDidBeginEditing(_ textField: UITextField)




instead to show picker, just write this line in your viewDidLoad



self.colorTxtField.inputView = self.colorPickerView






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 17:21









nikksindianikksindia

470411




470411













  • thank for your response , now I have got this error *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7ff65b720ef0> should have parent view controller:<project1.Add2ViewController: 0x7ff65b5292b0> but requested parent is:<UIInputWindowController: 0x7ff65c04e800>'

    – Auyem
    Jan 1 at 17:36











  • seems like your view hierarchy is not correct. Can you tell me how you are placing this textField and pickerView in your viewController ?

    – nikksindia
    Jan 1 at 17:40



















  • thank for your response , now I have got this error *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7ff65b720ef0> should have parent view controller:<project1.Add2ViewController: 0x7ff65b5292b0> but requested parent is:<UIInputWindowController: 0x7ff65c04e800>'

    – Auyem
    Jan 1 at 17:36











  • seems like your view hierarchy is not correct. Can you tell me how you are placing this textField and pickerView in your viewController ?

    – nikksindia
    Jan 1 at 17:40

















thank for your response , now I have got this error *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7ff65b720ef0> should have parent view controller:<project1.Add2ViewController: 0x7ff65b5292b0> but requested parent is:<UIInputWindowController: 0x7ff65c04e800>'

– Auyem
Jan 1 at 17:36





thank for your response , now I have got this error *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7ff65b720ef0> should have parent view controller:<project1.Add2ViewController: 0x7ff65b5292b0> but requested parent is:<UIInputWindowController: 0x7ff65c04e800>'

– Auyem
Jan 1 at 17:36













seems like your view hierarchy is not correct. Can you tell me how you are placing this textField and pickerView in your viewController ?

– nikksindia
Jan 1 at 17:40





seems like your view hierarchy is not correct. Can you tell me how you are placing this textField and pickerView in your viewController ?

– nikksindia
Jan 1 at 17:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997005%2fhow-to-hide-keyboard-while-using-uipickerview-with-swift-4%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas