2017-11-07 23 views
5

は、私は、フレームキーボードをテキストビューの下に表示させるにはどうすればいいですか?

weak var activeField: UITextField? 

func textFieldDidEndEditing(textField: UITextField) { 
    self.activeField = nil 


} 
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    if textField==txtOTP { 
     txtOTP.errorMessage="" 
    } 
    return true 
} 
func textFieldDidBeginEditing(textField: UITextField) { 
    self.activeField = textField 

} 




func keyboardDidShow(notification: NSNotification) 
{ 
    if let activeField = self.activeField, 
     let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { 
     let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0) 
     self.scrollView.contentInset = contentInsets 
     self.scrollView.scrollIndicatorInsets = contentInsets 
     var aRect = self.view.frame 
     aRect.size.height -= keyboardSize.size.height 
     if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
      self.scrollView.scrollRectToVisible(activeField.frame, animated: true) 
     } 
    } 


} 

func keyboardWillBeHidden(notification: NSNotification) 
{ 
    let contentInsets = UIEdgeInsetsZero 
    self.scrollView.contentInset = contentInsets 
    self.scrollView.scrollIndicatorInsets = contentInsets 
} 

をオブザーバー()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Gold_Loan_First_ViewController.keyboardDidShow(_:)), name: UIKeyboardDidShowNotification, object: nil) 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Gold_Loan_First_ViewController.keyboardWillBeHidden(_:)), name: UIKeyboardWillHideNotification, object: nil) 

を追加し、更新ビューでしたロードに

を使用してテキストフィールドの下に表示されるキーボードを行っている。しかし、私はそれをどのように行うのかtextView。私は同じ効果を上げずにtextViewのdidBeginEditingを使って同じコードを試しました

+1

あなたはどのような問題に直面していますか?あなたがキーボードの高さを得ることができることを確認しようとすると? 'UIKeyboardFrameEndUserInfoKey 'を使用できない場合 –

+0

同じコードをテキストビューに使用した場合、キーボードはテキストビューをオーバーレイします。 –

+0

私はそれを試したが、高さを得る。 –

答えて

2

あなたのアプリで次のポッドを使用するのは簡単かつ簡単なコードです。あなたの問題は、全体のアプリのために解決されます

IQKeyboardManager.sharedManager().enable = true 

IQKeyboardManger

後で、アプリケーションの委任にしてdidfinishLaunchingメソッドのコードのこの2行を追加することだけでインポートする必要があります。

1

私はこのような状況を経験しました。このために最初に私は私のUiViewControllerに拡張子を追加しました:

extension CCViewController : UITextViewDelegate { 
     func textViewDidBeginEditing(_ textView: UITextView) { 
      // write code as per your requirements 
     } 

     func textViewDidEndEditing(_ textView: UITextView) { 
      // write code as per your requirements 
     } 

     func textViewDidChange(_ textView: UITextView) { 
      self.p_adjustMessageFieldFrameForTextView(textView) 
     } 
    } 

    // Defining the p_adjustMessageFieldFrameForTextView method 

    fileprivate func p_adjustMessageFieldFrameForTextView(_ textView : UITextView) { 
      var finalheight : CGFloat = textView.contentSize.height + 10 
      var kMaxMessageFieldHeight : CGFloat = 50 
      if (finalheight > kMaxMessageFieldHeight) { 
       finalheight = kMaxMessageFieldHeight; 
      } else if (finalheight < kCommentTextViewHeight){ 
       finalheight = kCommentTextViewHeight; 
      } 

      scrollView!.view.frame = CGRect(x: 0, y: kNavBarHeight + statuBarHeight, width: SCREEN_WIDTH,height: SCREEN_HEIGHT - finalheight - keyboardRect!.size.height - kNavBarHeight - statuBarHeight) 
// It is there for understanding that we have to calculate the exact frame of scroll view 

      commentTextView!.frame = CGRect(x: 0, y: bottomOfView(scrollView!.view), width: SCREEN_WIDTH, height: finalheight) 
      self.p_setContentOffsetWhenKeyboardIsVisible() 
     } 

    // Defining the p_setContentOffsetWhenKeyboardIsVisible method 

    fileprivate func p_setContentOffsetWhenKeyboardIsVisible() { 
      // here you can set the offset of your scroll view 
     } 

bottomView()というメソッドがあります:

func bottomOfView(_ view : UIView) -> CGFloat { 
     return view.frame.origin.y + view.frame.size.height 
    } 
1

サルマンGhumsaniは右です。

UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKeyに変更してください。

Apple Debeloper Document: Keyboard Notification User Info Keys

UIKeyboardFrameEndUserInfoKey

画面座標におけるキーボードのendingフレーム矩形を識別するCGRectを含むNSValueオブジェクトのキー。フレームの四角形は、デバイスの現在の向きを反映しています。

UIKeyboardFrameBeginUserInfoKey

画面座標におけるキーボードのstartingフレーム矩形を識別する CGRectを含む NSValueオブジェクトのキー。フレームの四角形は、デバイスの現在の向きを反映しています。

結論

uはUIKeyboardFrameBeginUserInfoKeyを使用するのであれば、uは0にキーボードの高さを取得する可能性があります。システムはUITextViewと考えられていました。

+0

私はそれを試した。それは動作しません。さらに、同じコードを持つテキストフィールドの高さを取得しています。 –

+0

1. 'UIKeyboardFrameBeginUserInfoKey'は' textfield'では動作しますが、 'textview'では動作しません。 – Codus

+0

2.私は試してみました。 'activeField'を' activeTextView'に変更するのを忘れましたか? – Codus

1

キーボードの正確な高さを見つけて、それをtextViewのボトム拘束に割り当てるか、textViewのy位置を減らします。 ように:

ステップ-1: はあなたのViewControllerの不動産keyboardHeightを作ります。

var keyboardHeight: CGFloat = 0.0

ステップ-2:のTextViewの下部に制約の@IBOutletしてください。

@IBOutlet weak var textViewBottomConstraint: NSLayoutConstraint!

ステップ-3:

fileprivate func addKeyboardNotification() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

fileprivate func removeKeyboardNotification() { 
    IQKeyboardManager.shared().isEnabled = true 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

コピー&ビューコントローラにこれらの関数を貼り付け、viewDidLoad()self.addKeyboardNotification()を呼び出して、あなたのViewControllerを

ステップ-4:

deinit { 
    self.removeKeyboardNotification() 
} 

また、このコードをviewControllerに追加します。

ステップ-5:

func keyboardWillShow(_ notification: Notification) { 
    if let keboardFrame = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue, self.keyboardHeight <= 0.0 { 
     self.keyboardHeight = keboardFrame.height + 45.0 //(Add 45 if your keyboard have toolBar if not then remove it) 
    } 

    UIView.animate(withDuration: 0.3, animations: { 
     self.textViewBottomConstraint.constant = self.keyboardHeight 
    }, completion: { (success) in 
    }) 
} 

func keyboardWillHide(_ notification: Notification) { 
    UIView.animate(withDuration: 0.3, animations: { 
     self.textViewBottomConstraint.constant = 0.0 
    }, completion: { (success) in 
    }) 
} 

それはキーボードでのTextViewを管理することです。 textViewBottomConstraintを使用しない場合は、textViewのyの位置で作業できます。

1

ファイルを使用することによって、単にTPKAScrollViewController.h & TPKAScrollViewController.mファイルを使用することができます。

これらのobjective-Cファイルを迅速なプロジェクトにドラッグすると、Create Bridgingが自動的に要求されます。ブリッジヘッダーを作成し、YourApp-Bridging-Header.hファイルに#import "TPKAScrollViewController.h"をインポートします。その後

enter image description here

、単にXIBであなたのscrollViewを選択し、以下の示すようTPKeyboardAvoidingScrollViewにそのクラスを変更。

enter image description here

+0

こんにちは@ Jeesson_7これを試してみてください100テキストフィールドとtextViewの%。 – Warewolf

関連する問題