成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

APP開發(fā)之IOSSWIFT如何隱藏鍵盤-IOS隱藏鍵盤全解

2023-02-22    分類: App開發(fā)

如果你是剛從網(wǎng)站建設行業(yè)轉(zhuǎn)到APP開發(fā),那么你會覺得不可思議,隱藏鍵盤這么簡單的事情,還要這樣大費周張。先不牢騷了,以上是創(chuàng)新互聯(lián)的解決經(jīng)驗:
正常情況下,只要觸摸焦點放在輸入框如textField\textView上,自動會彈出鍵盤,所以,隱藏鍵盤的最終方法就是讓控件失去焦點。textField.resignFirstResponder()
textView.resignFirstResponder()
用例一、TextField按Enter鍵隱藏實現(xiàn)委托UITextFieldDelegate 方法
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}用例二、點擊其他任意地方隱藏override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
self.textField.resignFirstResponder()
self.textView.resignFirstResponder()
}
用例三、點擊手勢隱藏let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard:")
self.view.addGestureRecognizer(tapGesture)
func hideKeyboard(sender: UITapGestureRecognizer) {
self.SearchBar1.resignFirstResponder()
}
用例四、滾動tableView隱藏func scrollViewDidScroll(scrollView: UIScrollView) {
self.SearchBar1.resignFirstResponder()
}

用例五、通過鍵盤通知事件在其上方添加按鈕
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//添加鍵盤彈出通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name: UIKeyboardDidShowNotification, object: nil)
//添加鍵盤收起同志
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
override func viewDidDisappear(animated: Bool) {
//取消通知
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardDidShow(notification:NSNotification) {
let keyBoardBounds = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let deltaY = keyBoardBounds.size.height
let frame_height = self.view.frame.size.height
let butheight = frame_height - deltaY - 40
let width = self.view.frame.size.width
let cancalButton = UIButton(frame: CGRectMake(0,butheight,width/2,40))
cancalButton.setTitle("取消", forState:UIControlState.Normal)
cancalButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
cancalButton.backgroundColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1)
cancalButton.addTarget(self, action: "hideKeyboard", forControlEvents: UIControlEvents.TouchUpInside)
cancalButton.tag = 9999991
self.view.addSubview(cancalButton)
let completEditButton = UIButton(frame: CGRectMake(width/2,butheight,width/2,40))
completEditButton.setTitle("完成", forState:UIControlState.Normal)
completEditButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
completEditButton.backgroundColor = UIColor.darkGrayColor()
completEditButton.addTarget(self, action: "completeKeyboard", forControlEvents: UIControlEvents.TouchUpInside)
completEditButton.tag = 9999992
self.view.addSubview(completEditButton)
}
func keyboardWillHide(notification:NSNotification) {
if self.view.viewWithTag(9999991) != nil {
let btnCancal = self.view.viewWithTag(9999991) as! UIButton
let btnComplete = self.view.viewWithTag(9999992) as! UIButton
btnCancal.removeFromSuperview()
btnComplete.removeFromSuperview()
}
}

網(wǎng)頁名稱:APP開發(fā)之IOSSWIFT如何隱藏鍵盤-IOS隱藏鍵盤全解
本文鏈接:http://jinyejixie.com/news/239064.html

網(wǎng)站建設、網(wǎng)絡推廣公司-創(chuàng)新互聯(lián),是專注品牌與效果的網(wǎng)站制作,網(wǎng)絡營銷seo公司;服務項目有App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設
通化县| 寿光市| 浦城县| 宜川县| 阳西县| 读书| 阿鲁科尔沁旗| 天等县| 内乡县| 诸城市| 井研县| 阜南县| 慈溪市| 通海县| 兖州市| 尤溪县| 维西| 巴楚县| 衡南县| 济源市| 额尔古纳市| 潍坊市| 巩留县| 济宁市| 贞丰县| 南开区| 新巴尔虎右旗| 青河县| 营口市| 望城县| 思茅市| 化德县| 宜丰县| 来凤县| 孝感市| 利川市| 平舆县| 钟山县| 辰溪县| 合肥市| 迁西县|