我需要的是在首次启动应用程序时显示的许可协议。简单的UITextView,文本后带有“同意”按钮。我需要它的大小等于屏幕大小,并且内容可以滚动。所有这些都发生在主ViewController的ViewDidLoad()方法中:
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的内容高度为1850.5。需要添加y位置等于1850.5的按钮。
let button = UIButton(frame:CGRectMake(0, LVSize.height, 300, 50))
button.setTitle("Agree", forState: .Normal)
button.backgroundColor = UIColor.blueColor()
licenseView.addSubview(button)
现在我们需要调整内容的大小以使按钮在文本后可见
licenseView.contentSize = CGSizeMake(licenseView.frame.size.width, LVSize.height+300)
licenseView.setNeedsDisplay()
licenseView.layoutIfNeeded()
self.navigationController!.view.addSubview(licenseView)
问题是contentSize已更改(如果我print(licenseView.contentSize)
将输出(320.0, 2150.5)
),但textView仍表现为其内容高度为1850.5-向下滚动时,我看到该按钮在可滚动区域之外。我错过了什么?
好的,似乎当我licenseView.layoutIfNeeded()
在UITextView中调用layoutSubviews()时,将contentSize重置为文本的大小。我不知道它到底使用什么逻辑
我可以通过编写基于UITextView的自定义类并覆盖layoutSubviews方法来解决我的问题,但是我通过以下方法解决了这个问题:
licenseView.contentInset.bottom = button.frame.size.height
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句