简体   繁体   English

以编程方式创建时 UITextView 不滚动

[英]UITextView not scrolling when created programmatically

I am creating UITextView programmatically.我正在以编程方式创建 UITextView。

content = UITextView(frame: CGRect(x: 0, y: header.frame.origin.y + header.frame.size.height, width: self.frame.size.width, height: self.frame.size.height - viewTop.frame.size.height))
content.font = UIFont.init(name: "OpenSans", size: 17)
content.textColor = UIColor.black
content.backgroundColor = UIColor.white
content.isUserInteractionEnabled = true
content.isScrollEnabled = true
content.isEditable = true
content.isSelectable = true
content.bounces = true
content.showsVerticalScrollIndicator = true  
self.addSubview(content)  

It is not scrolling though.虽然它没有滚动。

TextView will scroll only when the Content Size is greater then the textView's frame.只有当内容大小大于 textView 的框架时,TextView 才会滚动。 So on setting the textView's text with enough text should allow it to scroll.因此,在使用足够的文本设置 textView 的文本时,应该允许它滚动。

Seems like that the code for adding the textview is probably in the viewDidLoad method.似乎添加 textview 的代码可能在viewDidLoad方法中。 When you add any view in viewDidLoad method there are good chance that the view will not have proper frame and content size.当您在viewDidLoad方法中添加任何视图时,该视图很可能没有适当的框架和内容大小。 Also there are good chance that any other view can ovelap on that.任何其他观点也很有可能与之重叠。 You can check by debuging the view by clicking the您可以通过单击调试视图来检查 image in debug area.调试区中的图像。

If the above scenario is right then you can fix the problem by putting content.bringSubview(toFront: self.view) in viewDidApear method.如果上述情况是正确的,那么您可以通过将content.bringSubview(toFront: self.view)放入viewDidApear方法来解决问题。

You have added the content view to it self.您已将内容视图添加到它自己。 Change this to view and it will scroll.将此更改为查看,它将滚动。

view.addSubview(content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM