2017-08-23 3 views
0

私はautolayoutでそれを拘束しようとすると本当に変です。背景は右に配置されますが、内のテキストは適切ではありません。私は、UIViewを作成しようとし、代わりにUIViewに追加しようとしましたが、問題は依然として続きます。ここでUITextViewの配置はオートレイアウトで変わっています

ここUITextViewとUIViewの宣言は

let noteTextView: UITextView = 
{ 
    let tv = UITextView() 
    tv.textColor = UIColor.white 
    tv.backgroundColor = UIColor.clear 
    tv.font = UIFont.systemFont(ofSize: 16) 
    tv.isEditable = false 
    tv.isUserInteractionEnabled = true 

    return tv 
}() 

let backgroundView: UIView = 
{ 
    let view = UIView() 
    view.backgroundColor = UIColor.rgb(red: 156, green: 44, blue: 252) 

    return view 
}() 

されており、ここに制約(拡張子を使用して、アンカーと呼ばれる)である

view.addSubview(backgroundView) 
    backgroundView.addSubview(noteTextView) 

    backgroundView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 65, paddingLeft: 0, paddingRight: 0, paddingBottom: 0, width: 0, height: 80) 

    noteTextView.anchor(top: backgroundView.topAnchor, left: backgroundView.leftAnchor, bottom: backgroundView.bottomAnchor, right: backgroundView.rightAnchor, paddingTop: 2, paddingLeft: 5, paddingRight: 5, paddingBottom: 2, width: 0, height: 0) 

は、問題を実証するためのビデオです:https://streamable.com/y8v70

注:UITextViewをビューに追加した直後(UIVIewなし)、まったく同じことが起こりました。

+0

なぜUILabelを使用しないのですか? – TomCobo

+0

テキストがかなり長くなる可能性があり、データベースからテキストをフェッチするときにテキストの行数がわからないためです。 – Elhoej

答えて

1

無効にautomaticallyAdjustsScrollViewInsets

self.automaticallyAdjustsScrollViewInsets = false 

viewDidLoadでも試してみてください。

textView.textContainerInset = UIEdgeInsetsZero; 
textView.textContainer.lineFragmentPadding = 0; 

このヘルプは、があります。

+0

最初のオプションが機能しました!どうもありがとう! – Elhoej

0

静的な高さをUIViewまたはUITextViewに与えないでください。

は、この機能を試してみてください:

func textviewHeight() { 

     let contentSize = self.TextView.sizeThatFits(self.TextView.bounds.size) 
     var frame = self.TextView.frame 
     frame.size.height = contentSize.height 
     self.TextView.frame = frame 

     self.aspectRatioTextView = NSLayoutConstraint(item: self.TextView, attribute: .Height, relatedBy: .Equal, toItem: self.TextView, attribute: .Width, multiplier: TextView.bounds.height/TextView.bounds.width, constant: 1) 
     self.TextView.addConstraint(self.aspectRatioTextView!) 

     self.mainViewHeightConstraint.constant = self.mainViewHeightConstraint.constant + self.TextView.frame.height 
    } 

は、それはあなたを助けかもしれませ!!!!!

関連する問題