-2

I have a view controller named as TeamDetailsViewController. I added scroll view to the view controller programmatically and added image view, nameLabel, and UITextview as the subview of the scroll view. imageview and NameLabel are showing up but Text View is not showing up for an unknown reason. Please help. Here is my code.

I am displaying set of items like table view using UIcolectionview. When a user taps on a single cell, he/she redirects to new view controller where the info about user like image, name, bio is displayed. Bio is 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


    }

}
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
srithan
  • 39
  • 1
  • 7

1 Answers1

0

You need to set the bottomAnchor of textView. Also you need to provide its height and width explicitly or set the leadingAnchor and trailingAnchor.

P.S. If this does not solve your problem. Please try to add more details in your question (A sample UI which you want develop).

  • I edited the code. Textview is showing up but it is scrolling horizontally. Please help. I want it to scroll vertically only. – srithan Sep 04 '18 at 09:14
  • You should use `UILabel` for the text you want to display. First calculate the height and width of Label by text you want to display. After that create a UILabel and add it to your scrollview. There is no need to add textview for displaying text only. – Umer Afzal Sep 04 '18 at 09:47
  • https://stackoverflow.com/questions/30450434/figure-out-size-of-uilabel-based-on-string-in-swift/30450559 Check out this link on how to **Figure out size of UILabel based on String in Swift**. – Umer Afzal Sep 04 '18 at 09:51