2017-09-21 5 views
0

コンテンツに基づいてUITextViewの高さを更新しようとしています。プログラムでUITextViewの高さを更新する

let eventDetailInfoTextBox : UITextView = { 
    let textbox = UITextView() 
    textbox.backgroundColor = UIColor.clear 
    textbox.layer.borderWidth = 1 
    textbox.layer.borderColor = ColorPallet.AppTertiaryColor.cgColor 
    textbox.layer.cornerRadius = 10 
    textbox.setNeedsDisplay() 
    let contentSize = textbox.sizeThatFits(textbox.bounds.size) 
    textbox.isEditable = false 
    return textbox 
}() 

サブビューは、その後、(setupViewsに追加された)その定義と一緒に:私は、私のようなUITextViewを定義this solutionを見てきましたが、それは私の現在のコード(まだ学んSWIFT)

で動作させることはできませんsetupEventDetailInfoTextBoxへの呼び出しを使用して、ビュー内の位置()

fileprivate func setupEventDetailInfoTextbox() { 

    print("Event Detail Info Text Box Height: \(eventDetailInfoTextBox.contentSize.height)") 

    var frame = eventDetailInfoTextBox.frame 
    frame.size.height = eventDetailInfoTextBox.contentSize.height 
    eventDetailInfoTextBox.anchor(eventDetailMapView.bottomAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: 8, leftConstant: 10, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: frame.size.height) 

} 

.anchorへのコールは、アプリケーションフレームワークは、このlink介して見つけたことを構築し、基本的にのXcodeからローカル関数をラップすることができますに基づいています。私はこれが動作していることを知っているし、私のアプリ全体に同じ機能を繰り返し再利用している。

print文の出力は-8で、1行のテキストを表示するのに適した高さで表示されます(時には)。

テキストが1行以上ある場合、私のテキストボックスが大きくならない理由は誰にも見えますか?私はIOS 10、Xcodeの8を使用し、迅速な3

答えて

0

に書いている

は、これらのステップ

  • をフォローのTextViewの高さ制約アウトレットを作成します。
  • Textviewのコンテンツの高さを計算します。
  • TextView高さ制約定数値を更新します。
  • layoutIfNeeded()関数を呼び出します。

私はうまくいきたいと思います。

+0

お返事ありがとうございます。私が追加することを忘れたのは、レイアウトのためにストーリーボードを使用していないということです(したがって.anchorコード)。私の質問の一部は、あなたが提案する仕事をどこで行うのかということに関連しています。あなたはどんな指導もお願いしますか? –

0

静的なもの、下にあるもの、汚れたものが必要な場合は、すべてを正しい順序で配置する限り、プロセスを合理化できます(プロパティの設定とリセットは行いません)。デバイスのローテーションやその他の実行時の変更を処理する必要がある場合は、多少のダウンとダーティを必要としますが、多くは必要ありません。必要なものがわからないので、簡単に選択しました。

// instance property so that other constraints can refer to it 
let descriptionTextView = UITextView() 

// configure text view 
descriptionTextView.text = descriptionModel 
descriptionTextView.font = // UIFont 
descriptionTextView.textColor = // UIColor 
descriptionTextView.isEditable = false 
descriptionTextView.translatesAutoresizingMaskIntoConstraints = false 
view.addSubview(descriptionTextView) 

// set constraints 
descriptionTextView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true 
descriptionTextView.topAnchor.constraint(equalTo: divider.bottomAnchor, constant: 32).isActive = true 
descriptionTextView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -32).isActive = true 
descriptionTextView.sizeToFit() 
descriptionTextView.layoutIfNeeded() 
descriptionTextView.heightAnchor.constraint(equalToConstant: descriptionTextView.contentSize.height).isActive = true 
関連する問題