2017-08-04 15 views
0

キーボードでシフトアップではない、ここに私の問題ですスタックビュー私はいくつかの異なるものを含むUIViewを持っています。ボトムビューでは、私はUITextViewを持っています。私が理想的には、入力を開始するときに上に移動したいので、実際に私が入力しているものを見ることができます。はUITextView私はSOこれを理解しようとする日の大半を費やしてきた

実装ソリューションはherehereherehereを発見した、と私は代わりにUITextViewの最後のビューでUITextFieldを持っている場合は、このトピックに関する他のすべての同様の前の質問には動作しますが、私は本当に、リターンキーを持っていると思います/マルチライン機能は、UITextViewを必要とするようです。ここで

は、私は(いくつかの上記のリンクあたり)コードの面で持っているものです。

func registerForKeyboardNotifications() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil) 
} 

func keyboardWasShown(_ notification: NSNotification) { 

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } 

    let keyboardFrame = keyboardFrameValue.cgRectValue 
    let keyboardSize = keyboardFrame.size 

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

} 

func keyboardWillBeHidden(_ notification: NSNotification) { 
    let contentInsets = UIEdgeInsets.zero 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 
} 

私はviewDidLoad()registerForKeyboardNotifications()と呼ばれてきたが、これは私がUITextFieldを使用する場合完璧を作業していて、ためていますどんな理由であれ、機能はUITextViewで動作しません。問題に最も近いSOの質問はthis oneですが、問題はUITextViewを含む実行可能な解決策で決して答えられませんでした。私はいくつかの投稿IQKeyboardManagerを参照して見てきましたが、私は理想的にこれをネイティブに修正したいと思います。

答えて

0

私はcontentInsetsetContentOffsetを組み合わせた解決策を見つけましたが、それはそこで最もエレガントなものではないと確信しています。ただし、他の人がkeyBoardWasShownに移動するにはtextViewを取得できない場合は、次の操作を実行できます。

viewControllerにキーボードが表示されたときに通知するように設定します。

func registerForKeyboardNotifications() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil) 
} 

func keyboardWasShown(_ notification: NSNotification) { 

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } 

    let keyboardFrame = keyboardFrameValue.cgRectValue 
    let keyboardSize = keyboardFrame.size 

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

} 

func keyboardWillBeHidden(_ notification: NSNotification) { 
    let contentInsets = UIEdgeInsets.zero 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 
} 

viewDidLoadregisterForKeyboardNotifications()を呼び出すようにしてください。

これは、設定によって異なります。私は(IBOutletにしました)が私のtextViewの一部を見せてくれるように飛び跳ねて、ユーザーがそこにいて編集可能であることを知りたかっただけでした。これは、キーボードの表示/非表示の状態を扱うコードの上記部分に以下の変更を加えました。

func keyboardWasShown(_ notification: NSNotification) { 

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } 

    let keyboardFrame = keyboardFrameValue.cgRectValue 
    let keyboardSize = keyboardFrame.size 

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height - 50.0, 0.0) 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

    let bottom = keyboardSize.height - 60.0 
    scrollView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true) 

} 

func keyboardWillBeHidden(_ notification: NSNotification) { 

    let contentInsets = UIEdgeInsets.zero 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

} 

キー項目はscrollView.setContentOffset部分です。

関連する問題