2016-03-21 50 views
0

我嘗試使用某些東西向上移動視圖,因爲當我走到表單末尾並點擊最後一個文本框時,我看不到我寫入的內容,因爲鍵盤出現在文本字段,所以我找到的東西,但在這裏,如果我上認爲這一個要去要去躲通過進入上部頂端的文本框挖掘,這是代碼:將鍵盤上方的文本字段向上移動

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) { 

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { 
     self.view.frame.origin.y -= keyboardSize.height 
    } 

} 

func keyboardWillHide(notification: NSNotification) { 
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { 
     self.view.frame.origin.y += keyboardSize.height 
    } 
} 

但這裏有一個又一個的問題,當我點擊兩次另一個文本框時,會出現一個黑色框。

所以我想做的是檢測文本字段,並把鍵盤放在它下面,所以向上移動文本字段。並檢測鍵盤是否已經在這裏做不顯示黑色集團。但我還沒有找到解決方案。

+1

使用https://github.com/hackiftekhar/IQKeyboardManager或https://github.com/michaeltyson/TPKeyboardAvoiding ...處理鍵盤本身 –

+1

@EICaptain沒有使用庫的解決方案沒有解決方案? – Ben

+0

你可以嘗試TPKeyboardAvoiding第三方庫來解決它https://github.com/michaeltyson/TPKeyboardAvoiding – Birendra

回答

2

我已經在我的項目中使用此代碼:

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(true) 

    // Register Keyboard Notification 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) 
} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 

    // Remove Keyboard notification observer 
    NSNotificationCenter.defaultCenter().removeObserver(self) 
} 

func keyboardWillShow(notification: NSNotification) 
{ 
    var userInfo = notification.userInfo! 
    var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue() 
    keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil) 

    var contentInset:UIEdgeInsets = self.scrollView.contentInset 
    contentInset.bottom = keyboardFrame.size.height - 30 //Set this value (30) according to your code as i have navigation tool bar for next and prev. 
    self.scrollView.contentInset = contentInset 
} 

func keyboardWillHide(notification: NSNotification) { 
    let contentInset:UIEdgeInsets = UIEdgeInsetsZero 
    self.scrollView.contentInset = contentInset 
} 

希望它會幫助你!

+1

它幫助我謝謝 – Ben

1

添加一個布爾值屬性self.isKeyBoardUp

在keyboardWillShow: 如果isKeyBoardUp回報,否則設置爲true,做你的東西。

在keyboardWillHide: 如果不是isKeyBoardUp返回,否則將其設置爲false,並做你的東西。

+0

當然,但我也不想把鍵盤放在文本框下 – Ben

1

請檢查驗證碼我正在嘗試,並適用於獲得這一個從

Move textfield when keyboard appears swift

func keyboardWasShown(notification: NSNotification) { 
    var info = notification.userInfo! 
    var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 

    UIView.animateWithDuration(0.1, animations: {() -> Void in 
    self.bottomConstraint.constant = keyboardFrame.size.height + 20 
    }) 
} 
+0

我正在學習意見,但我不明白什麼是約束 – Ben

相關問題