2016-07-30 13 views
1

私は鉱山のような投稿がいくつかあると知っていますが、探しているものではありません。だからここで私が達成しようとしていることがあります。私はこのようなViewControllerを持っています。 UITextViewの高さを動的に調整する方法

enter image description here

現在のテキスト「説明」を持っているUITextViewへのユーザ入力のテキストは、私はテキストがどこ複数行になったり、画面をオフに行くためにそうならば、動的UITextViewの高さを変更したい

ユーザはその内容で全体のビューをスクロールすることができる。ここに私のプログラムです。それは UITextViewフレームを調整し、 UIScrollView.contentSize(未UITextViewの)を更新するので、ユーザーは全体のビューをスクロールすることができますように

@IBOutlet var scrollView: UIScrollView! 
var projectNameTextField: UITextField! 
var projectCategoryLabel: UIButton! 
var dueDateLabel: UIButton! 
var projectDescription: UITextView! 
var projectDescriptionFrame: CGRect! 

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

    let scrollViewWidth = scrollView.frame.width 
    scrollView.contentSize.width = scrollViewWidth 

    let trallingSpace: CGFloat = 12.0 
    let contentWidth = scrollViewWidth - (trallingSpace * 2.0) 

    projectNameTextField = UITextField(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40)) 
    projectNameTextField.placeholder = "Title" 
    updateScrollView(withView: projectNameTextField) 


    let categoryLabel = UILabel() 
    categoryLabel.text = "Category" 

    projectCategoryLabel = UIButton() 
    projectCategoryLabel.setTitle("Art", forState: .Normal) 
    projectCategoryLabel.setTitleColor(UIColor.blackColor(), forState: .Normal) 

    let stackViewHCategory = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40)) 
    stackViewHCategory.axis = .Horizontal 
    stackViewHCategory.spacing = 8 
    stackViewHCategory.addArrangedSubview(categoryLabel) 
    stackViewHCategory.addArrangedSubview(projectCategoryLabel) 
    updateScrollView(withView: stackViewHCategory) 


    let dueDateTitle = UILabel() 
    dueDateTitle.text = "Due Date" 

    dueDateLabel = UIButton() 
    dueDateLabel.setTitle("No Due Date", forState: .Normal) 
    dueDateLabel.setTitleColor(UIColor.blackColor(), forState: .Normal) 

    let stackViewHDueDate = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40)) 
    stackViewHDueDate.axis = .Horizontal 
    stackViewHDueDate.spacing = 8 
    stackViewHDueDate.addArrangedSubview(dueDateTitle) 
    stackViewHDueDate.addArrangedSubview(dueDateLabel) 
    updateScrollView(withView: stackViewHDueDate) 


    projectDescription = UITextView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40)) 
    projectDescription.text = "Description" 

    projectDescription.delegate = self 

    projectDescription.scrollEnabled = false 
    projectDescription.font = projectDescription.font?.fontWithSize(14) 

    updateScrollView(withView: projectDescription) 

    projectDescriptionFrame = projectDescription.frame 
    projectDescriptionFrame.size.height = projectDescription.contentSize.height 
    projectDescription.frame = projectDescriptionFrame 
} 

func textViewDidChange(textView: UITextView) { 

    projectDescriptionFrame.size.height = projectDescription.contentSize.height 
    projectDescription.frame = projectDescriptionFrame 

    var contentSizeheight = CGFloat() 

    for (index, viewInScrollView) in scrollView.subviews.enumerate() { 
     if index > 1 { 
      contentSizeheight += viewInScrollView.frame.height 
     } 
    } 

    scrollView.contentSize.height = contentSizeheight 
} 


func updateScrollView(withView withView: UIView) { 
    scrollView.addSubview(withView) 
    scrollView.contentSize.height += withView.frame.height 
} 

func scrollViewContentHeight() -> CGFloat { 

    var height = CGFloat() 

    for (index, viewInScrollView) in scrollView.subviews.enumerate() { 
     if index > 1 { 
      height += viewInScrollView.frame.height 
     } 
    } 

    return height 
} 

は、どのように私はこれを行うことができます。そして、私はあなたがiOS Mailアプリで新しいメッセージを作成するときのようなUIが欲しいと言っています。そこでは、電子メールの件名とメッセージの本文を1つにスクロールすることができます。これで私を助けてください。私は数日間このことに固執しています。

enter image description here

答えて

0

わかりました、私はそれを考え出しました。ここに私が変えたものがあります。

グローバルプロパティです。

var oldProjectDescriptionHeight: CGFloat! 

var projectDescriptionHeight: CGFloat! { 
    willSet { 
     oldProjectDescriptionHeight = projectDescriptionHeight 
    } 
} 

そしてUITextViewDelegatetextViewDidChange

func textViewDidChange(textView: UITextView) { 

    let fixedWidth = textView.frame.size.width 
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max)) 
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max)) 
    var newFrame = textView.frame 
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height) 
    textView.frame = newFrame 

    projectDescriptionHeight = newFrame.height 

    scrollView.contentSize.height += projectDescriptionHeight - oldProjectDescriptionHeight 
} 
関連する問題