2016-01-21 24 views
0
關閉鍵盤

我試圖編程方式關閉屏幕上的鍵盤和我有我的代碼如下無法爲iPhone 5和iPhone5s

override func viewDidLoad() { 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); 
} 

func keyboardWillShow(notification: NSNotification) { 
    var info = notification.userInfo! 
    let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
    UIView.animateWithDuration(0.1, animations: {() -> Void in 
     self.Bottomspace.constant = keyboardFrame.size.height 
    }) 
} 

func keyboardWillHide(notification: NSNotification){ 
    self.Bottomspace.constant = 0 
} 

它做工精細的iPhone 6和iPad,而不是iPhone 5和iPhone 5s,有什麼建議嗎?

+0

就在您點擊UITextField外部時? – senty

+0

諾普,我做鍵盤上的返回鍵 –

回答

1

試試這個:

// Dismiss Keyboard when user touch screen 
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     self.view.endEditing(true) 
    } 

讓我知道,如果它的工作原理!

+0

啊,它的工作原理!非常感謝你! –

1

添加UITextFieldDelegate到類聲明:

class ViewController: UIViewController, UITextFieldDelegate 

連接文本框或者編程寫

@IBOutlet weak var userText: UITextField! 

設置您的視圖控制器在視圖中的文本字段代表做負載:

override func viewDidLoad() { 
super.viewDidLoad() 
self.userText.delegate = self 
} 

添加此代表

func textFieldShouldReturn(userText: UITextField!) -> Bool { 
yourtextfield.resignFirstResponder() 
return true; 
}