国产精品一区二区三区……-大杳蕉伊人欧美一本遒在饯-日本不卡一区免费在线观看-国产亚洲欧美中文字幕

400-800-9385
網(wǎng)站建設(shè)資訊詳細(xì)

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

發(fā)表日期:2016-01-07 00:00:00   作者來源:方維網(wǎng)絡(luò)   瀏覽:7143   標(biāo)簽:APP開發(fā)    IOS    

如果你是剛從網(wǎng)站建設(shè)行業(yè)轉(zhuǎn)到APP開發(fā),那么你會覺得不可思議,隱藏鍵盤這么簡單的事情,還要這樣大費周張。先不牢騷了,以上是方維網(wǎng)絡(luò)的解決經(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<UITouch>, 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()
}
 

用例五、通過鍵盤通知事件在其上方添加按鈕


IOS鍵盤
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)絡(luò)原創(chuàng),轉(zhuǎn)載請注明來自http://pdcharm.com/news/2846.html
宁城县| 古丈县| 长岭县| 姚安县| 土默特右旗| 宁蒗| 天门市| 汉中市| 营口市| 峨眉山市| 新巴尔虎右旗| 贺兰县| 花垣县| 台州市| 玉环县| 梁河县| 彰武县| 紫金县| 怀柔区| 武鸣县| 伊宁县| 太和县| 黔江区| 兰州市| 乌审旗| 休宁县| 江西省| 扶沟县| 汕头市| 个旧市| 马尔康县| 荃湾区| 灵寿县| 南皮县| 黎城县| 瑞金市| 灌云县| 陵川县| 新津县| 思茅市| 双鸭山市|