Ios 文本视图未在scrollview-Swift 4中垂直滚动

Ios 文本视图未在scrollview-Swift 4中垂直滚动,ios,swift,uiscrollview,uitextview,Ios,Swift,Uiscrollview,Uitextview,我有一个名为TeamDetailsViewController的视图控制器。我以编程方式将scroll view添加到视图控制器,并将image view、nameLabel和UITextview添加为scroll view的子视图。imageview和NameLabel正在显示,但文本视图由于未知原因未显示。请帮忙。这是我的密码 我正在使用UICollectionView显示一组项目,如表视图。当用户点击单个单元格时,他/她将重定向到新的视图控制器,在该控制器中显示有关用户的信息,如图像、姓名

我有一个名为TeamDetailsViewController的视图控制器。我以编程方式将scroll view添加到视图控制器,并将image view、nameLabel和UITextview添加为scroll view的子视图。imageview和NameLabel正在显示,但文本视图由于未知原因未显示。请帮忙。这是我的密码

我正在使用UICollectionView显示一组项目,如表视图。当用户点击单个单元格时,他/她将重定向到新的视图控制器,在该控制器中显示有关用户的信息,如图像、姓名、个人信息。Bio是
TextView

class TeamDetailsController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        print(textView.text)
    }

    lazy var scrollView: UIScrollView = {
        let sv = UIScrollView(frame: self.view.bounds)
        sv.backgroundColor = .white
        sv.translatesAutoresizingMaskIntoConstraints = false
        sv.layer.borderWidth = 5
        return sv
    }()

    let profileImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "team-tony")
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 50
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()

    let nameLabel: UILabel = {
        let label = UILabel()
        label.text = "Jared 'Donald' Dunn"
        label.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    let textView: UITextView = {
        var tv = UITextView()
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.textColor = .black
        tv.font = UIFont.systemFont(ofSize: 16)
        tv.isEditable = false
        return tv
    }()

    func setupViews() {

        view.backgroundColor = .white
        view.addSubview(scrollView)

        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        scrollView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
        scrollView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true

        scrollView.addSubview(profileImageView)
        profileImageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
        profileImageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 20).isActive = true

        scrollView.addSubview(nameLabel)
        nameLabel.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
        nameLabel.topAnchor.constraint(equalTo: profileImageView.bottomAnchor, constant: 10).isActive = true

        scrollView.addSubview(textView)
        textView.topAnchor.constraint(equalTo: nameLabel.bottomAnchor).isActive = true
        textView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
        textView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 20).isActive = true
        textView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 20).isActive = true


    }

}

您需要设置textView的底部锚定。此外,您还需要明确提供其高度和宽度,或设置引导锚牵引锚


如果这不能解决你的问题。请尝试在您的问题中添加更多细节(您希望开发的示例UI)。

您需要添加
textView
bottomAnchor
,或者需要设置
textView
高度。@Kuldeep如何设置动态高度只设置textView的bottomAnchor。@Pankil您能告诉我底部anchor的代码吗。我是否应该将其设置为scrollview.BottomAnchories尝试将scrollview底部设置为TextView
TextView.bottomAnchor.constraint(equalTo:scrollview.bottomAnchor).isActive=true
我编辑了代码。Textview正在显示,但正在水平滚动。请帮忙。我只希望它垂直滚动。您应该对要显示的文本使用
UILabel
。首先根据要显示的文本计算标签的高度和宽度。然后创建一个UILabel并将其添加到scrollview中。只显示文本不需要添加textview。查看此链接,了解如何根据Swift中的字符串计算UILabel的大小。