虛擬鍵盤隱藏

Doo
Doo
Mar 22, 2017 · 2 min read

當我們在進行iOS的使用者界面設計時,經常會用到Text Field來讓使用者進行輸入,當在Text Field輸入時,系統會自動的帶出虛擬鍵盤來讓使用者輸(Simulator -> Hardware ->Keyborad -> Toggle Software Keyboard 需打勺),但鍵盤不會自動隱藏,需要從程式定義隱藏的時機。

1 按下鍵盤return時隱藏:

若使用Interface Builder建立Text Field,必須遵從UITextFieldDelegate協定,且拉藍線到File’s Owner跟delegate產生關係,便可實作SDK裡的方法

Image for post
Image for post

畫面上的Text Field(按右鍵)跟delegate產生關係

Image for post
Image for post

按下鍵盤上的「return」後隱藏虛擬鍵盤

ViewController.swift 加入以下程式碼

Image for post
Image for post
Image for post
Image for post

2點擊背景後收起鍵盤:

ViewController.swift 加入以下程式碼

func 前,記得加入 override。

Image for post
Image for post
Image for post
Image for post

彼得潘的 Swift iOS App 開發教室

學習 Swift iOS App 開發的學生作品集

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store