2016-05-30 21 views
5

UITextViewをUIStackViewの中に追加しました。UITextview in UIStackView

しかし、私のtextviewは複数行ではありません。

オートレイアウトは以下の通りです:それはちょうど1行を取られ

enter image description here

画像を1として。テキストビューフレームは、イメージごとにフレームの外に表示されます。

UITextView

enter image description here enter image description here

UIStackView

enter image description here

+0

問題は何ですか? – Lion

+0

@Lion、複数行で表示する必要がありますか? – user2526811

答えて

14

問題は、heightのUIStackviewを固定しているということですの先頭にの下にという制約があります。

  1. 制約

    enter image description here

を無効のTextViewをスクロールを削除します。

は、自己にあなたののTextViewデリゲートを作成し、textViewDidChange

スウィフト

func textViewDidChange(_ textView: UITextView) { 
    view.layoutIfNeeded() 
} 

Objective Cの実装:

-(void)textViewDidChange:(UITextView *)textView { 
    [self.view layoutIfNeeded]; 
} 

をこのメソッドが呼び出されることを確認します。

ここで、stackviewはtextviewとともに複数行になるはずです。

テキストビューが複数行でない理由は、高さを固定しているためです。ですから、それは決して複数行になることはありません。

はGIFを参照してください:私はこれまでのところ、このために発見した

enter image description here

+1

優秀!静的なテキストがある場合は、viewDidLayoutSubviews()をオーバーライドしてコンテンツの高さを取得し、高さの制約を更新できることがわかりました。しかし、あなたの答えは正しい道のりで私を得ました! \tオーバーライドFUNCのviewDidLayoutSubviews(){ \t \t super.viewDidLayoutSubviews() \t \t textViewHeightConstraint.constant = textView.contentSize.height \t \t textView.layoutIfNeeded() \t} –

0

最良の解決策は、UIViewの中UITextViewをラップすることであり、その後、高アンカーとUITextViewの固定の高さを設定します。

let textView = UITextView() 
let containerView = UIView() 
textView.translatesAutoresizingMaskIntoConstraints = false 
containerView.addSubview(textView) 

textView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true 
textView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true 
textView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true 
textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true 

textView.heightAnchor.constraint(equalToConstant: 100).isActive = true 
let stackView = UIStackView(arrangedSubviews: [containerView])