Reputation: 629
What I need is license agreement to be shown up on the first launch of application. Simple UITextView with with agree button after text. I need that its size to be equal screen size and content could be scrollable. All this happens in ViewDidLoad() method in main ViewController:
let text = "<p>very very long html text</p>"
var str = NSAttributedString()
do {
str = try NSAttributedString(data: text.dataUsingEncoding
(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
}
catch
{
print(error)
}
let licenseView = UITextView(frame:CGRectMake(self.navigationController!.view.frame.origin.x,self.navigationController!.view.frame.origin.y, self.navigationController!.view.frame.size.width, self.navigationController!.view.frame.size.height))
licenseView.attributedText = str
licenseView.scrollEnabled = true
licenseView.editable = false
licenseView.selectable = true
licenseView.dataDetectorTypes = .All
let LVSize:CGSize = licenseView.sizeThatFits(CGSizeMake(licenseView.frame.size.width, CGFloat(FLT_MAX)))
licenseView' content height is 1850.5. Need to add button with y position which equals to 1850.5.
let button = UIButton(frame:CGRectMake(0, LVSize.height, 300, 50))
button.setTitle("Agree", forState: .Normal)
button.backgroundColor = UIColor.blueColor()
licenseView.addSubview(button)
now we need to resize content to make button visible after text
licenseView.contentSize = CGSizeMake(licenseView.frame.size.width, LVSize.height+300)
licenseView.setNeedsDisplay()
licenseView.layoutIfNeeded()
self.navigationController!.view.addSubview(licenseView)
The problem is that contentSize is changed(if I print(licenseView.contentSize)
it will output (320.0, 2150.5)
), but the textView still behave like its content height is 1850.5 - when I scroll down I see that button is outside of scrollable area. What I've missed?
Upvotes: 2
Views: 382
Reputation: 629
Ok, it's seems that when I called licenseView.layoutIfNeeded()
layoutSubviews() in UITextView reseted contentSize to the size of text. I don't know what logic it uses exactly
I could solve my problem with writing custom class based on UITextView and overwriting layoutSubviews method but I solved the problem with this:
licenseView.contentInset.bottom = button.frame.size.height
Upvotes: 0
Reputation: 373
You can do it if you have AutoLayout
in your storyboard. You need to create outlet of your height and width constraints.
@IBOutlet weak var height: NSLayoutConstraint!
@IBOutlet weak var width: NSLayoutConstraint!
Now you can change it as your wish by assigning constant value to it
width.constant = 400
height.constant = 200
Hope this could help you!!
Upvotes: 2