滚动视图上不显示文本视图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了滚动视图上不显示文本视图相关的知识,希望对你有一定的参考价值。
我有一个视图控制器名称为TeamDetailsViewController。我以编程方式将滚动视图添加到视图控制器,并添加了UITextview作为滚动视图的子视图。但是文本视图并没有出现未知原因。请帮忙。这是我的代码
class TeamDetailsController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = false
setupViews()
}
lazy var scrollView: UIScrollView = {
let sv = UIScrollView(frame: self.view.bounds)
sv.backgroundColor = .blue
sv.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleHeight.rawValue)))
sv.contentSize = CGSize(width: self.view.bounds.size.width, height: self.view.bounds.size.height * 2);
return sv
}()
var textView: UITextView = {
var tv = UITextView()
tv.textColor = .darkGray
tv.backgroundColor = .black
tv.font = UIFont.systemFont(ofSize: 16)
return tv
}()
func setupViews() {
view.backgroundColor = .white
view.addSubview(scrollView)
scrollView.addSubview(textView)
scrollView.addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: textView)
scrollView.addConstraintsWithFormat(format: "V:|-16-[v0]-16-|", views: textView)
}
}
答案
试试这个代码;
var textView: UITextView = {
var tv = UITextView()
tv.textColor = .darkGray
tv.backgroundColor = .black
tv.translatesAutoresizingMaskIntoConstraints = false
tv.font = UIFont.systemFont(ofSize: 16)
return tv
}()
另一答案
您将scrollView contentSize显式设置为此变体中的框架,您不应使用具有scrollView子视图的自动布局约束。试试这个:
func setupViews() {
view.backgroundColor = .white
view.addSubview(scrollView)
scrollView.addSubview(textView)
textView.frame = CGRect.init(origin: CGPoint(0, 0), size: scrollView.contentSize).insetBy(dx: 16, dy: 16)
}
另一个变体是不将scrollView contentSize显式设置为frame,并将scrollView子视图约束设置为scrollView的边缘,并使用约束设置此subViews的高度和宽度,scrollView将从该宽度和高度约束获取内容大小。例:
view.addSubview(scrollView)
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
scrollView.addSubview(imageView)
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
imageView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 0).isActive = true
imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
imageView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 0).isActive = true
imageView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
imageView.heightAnchor.constraint(equalToConstant: view.frame.height - 32).isActive = true
别忘了设置scrollViews及其子视图
scrollView.translatesAutoresizingMaskIntoConstraints = false
imageView.translatesAutoresizingMaskIntoConstraints = false
以上是关于滚动视图上不显示文本视图的主要内容,如果未能解决你的问题,请参考以下文章