国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

Swift讓輸入框跟隨鍵盤彈起避免輸入輸入法擋住輸入框問題

2020-03-09 17:47:07
字體:
來源:轉載
供稿:網友

第一步: 新建Controller

在Xcode選擇File → New → File → Cocoa Touch Class

新建LoginViewController繼承自UIViewController

第二步:創建兩個UITextField

passwordInput: UITextField // 密碼輸入框
accountInput: UITextField // 帳號輸入框

第三步:添加鍵盤KVO

在viewDidLoad方法添加下面兩行代碼

//當鍵盤彈起的時候會向系統發出一個通知,//這個時候需要注冊一個監聽器響應該通知NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil)//當鍵盤收起的時候會向系統發出一個通知,//這個時候需要注冊另外一個監聽器響應該通知NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil)

添加全局控制參數

因為連續在兩個或多個textfield之間切換時候,只會發送UIKeyboardWillShowNotification鍵盤顯示通知,而不會發送UIKeyboardWillHideNotification鍵盤隱藏通知,這就需要一個全局參數控制鍵盤只在第一次點擊輸入框時候界面上移,該參數變為false,光標移到另一個輸入框時界面不再變化。當關閉鍵盤時候,界面下移,并將這個參數恢復為默認值。

在類的第一行聲明該變量:

var keyBoardNeedLayout: Bool = true

添加兩個方法分別相應鍵盤彈起和鍵盤隱藏

鍵盤彈起響應

func keyboardWillShow(notification: NSNotification) {print("show")if let userInfo = notification.userInfo,value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {let frame = value.CGRectValue()let intersection = CGRectIntersection(frame, self.view.frame)let deltaY = CGRectGetHeight(intersection)if keyBoardNeedLayout {UIView.animateWithDuration(duration, delay: 0.0,options: UIViewAnimationOptions(rawValue: curve),animations: { _ inself.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)self.keyBoardNeedLayout = falseself.view.layoutIfNeeded()}, completion: nil)}}}

鍵盤隱藏響應

func keyboardWillHide(notification: NSNotification) {print("hide")if let userInfo = notification.userInfo,value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {let frame = value.CGRectValue()let intersection = CGRectIntersection(frame, self.view.frame)let deltaY = CGRectGetHeight(intersection)UIView.animateWithDuration(duration, delay: 0.0,options: UIViewAnimationOptions(rawValue: curve),animations: { _ inself.view.frame = CGRectMake(0,deltaY,self.view.bounds.width,self.view.bounds.height)self.keyBoardNeedLayout = trueself.view.layoutIfNeeded()}, completion: nil)}}

更進一步

如果輸入框吸底,y的位移可以用-deltaY

self.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)

但是如果輸入框在偏上的位置就有可能導致某個輸入框移出界面視界,這時候可以把位移寫成deltaY/2或者deltaY/4等,自己去嘗試吧。

以上所述是小編給大家介紹的Swift讓輸入框跟隨鍵盤彈起避免輸入輸入法擋住輸入框問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹤壁市| 丰都县| 东宁县| 临安市| 宜昌市| 新营市| 阳西县| 讷河市| 山西省| 明光市| 江源县| 井冈山市| 嫩江县| 汉源县| 清水县| 哈尔滨市| 永丰县| 乳山市| 武鸣县| 浪卡子县| 江口县| 马边| 和政县| 富源县| 潜山县| 沧州市| 荆门市| 松原市| 仪陇县| 汝州市| 墨竹工卡县| 福州市| 邓州市| 芦山县| 麻栗坡县| 青铜峡市| 江孜县| 渝北区| 随州市| 芜湖县| 博乐市|