-1

So I've added a scrollview to my view controller programmatically and added some views.

func setupScrollView() {
    view.addSubview(scrollView)
    scrollView.addSubview(contentView)
    scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
    contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
    contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
    scrollView.contentSize = contentView.frame.size
}

func addUserAndFollowView(id: String) {
    userAndFollow = UserPfAndFollow(id: id)
    if let userAndFollow = userAndFollow {
        userAndFollow.view.isUserInteractionEnabled = true
        contentView.addSubview(userAndFollow.view)
        self.contentView.bringSubviewToFront(userAndFollow.view)
        userAndFollow.view.translatesAutoresizingMaskIntoConstraints = false
        userAndFollow.view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        userAndFollow.view.widthAnchor.constraint(equalTo: contentView.widthAnchor).isActive = true
        userAndFollow.view.heightAnchor.constraint(equalToConstant: 100).isActive = true
        
    }
    
}

func setImageViewConstraints() {
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.heightAnchor.constraint(equalToConstant: 300).isActive = true
    imageView.widthAnchor.constraint(equalToConstant: 300).isActive = true
    imageView.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor).isActive = true
    if let userAndFollow = userAndFollow?.view {
        imageView.topAnchor.constraint(equalTo: userAndFollow.bottomAnchor, constant: 5).isActive = true
    }
}

func addLabelConstraints() {
    self.albumTitle?.translatesAutoresizingMaskIntoConstraints = false
    self.albumDescription?.translatesAutoresizingMaskIntoConstraints = false
    albumTitle?.numberOfLines = 2
    albumDescription?.numberOfLines = 3
    albumDescription?.adjustsFontSizeToFitWidth = false

    albumTitle?.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 14).isActive = true    
    albumTitle?.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10).isActive = true
    albumTitle?.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
    albumTitle?.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10).isActive = true
   
    albumDescription?.lineBreakMode = .byTruncatingTail
    albumDescription?.topAnchor.constraint(equalTo: albumTitle!.bottomAnchor, constant: 9).isActive = true
    albumDescription?.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10).isActive = true
    albumDescription?.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
}

All these views show up the way I want them to with the scrollview not scrolling but when I add this next view (which is a tableview in a view controller) at the bottom of the rest of my views it doesn't show up. Possibly why the scrollview isn't scrolling.

func addViewController() {
        if let viewController = viewController {
            contentView.addSubview(viewController.view)
            setVCConstraints()
        }
        
    }
    
    func setVCConstraints() {
        viewController?.view.translatesAutoresizingMaskIntoConstraints = false
        viewController?.view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
        
        viewController?.view.topAnchor.constraint(equalTo: albumDescription!.bottomAnchor, constant: 7).isActive = true
        
        viewController?.view.widthAnchor.constraint(equalTo: self.contentView.widthAnchor).isActive = true
        
    }

What can I do to make this view appear and have my scrollview scroll all the way down this view controller and it's array content and no more or less?

DDavis25
  • 1,149
  • 1
  • 13
  • 25

1 Answers1

0

Before you add the view, you have

contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
scrollView.contentSize = contentView.frame.size

So, of course, the contentView has a size of the scrollView and thus will not scroll. It is the same size.

After you add the view, you have

viewController?.view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
        
viewController?.view.topAnchor.constraint(equalTo: albumDescription!.bottomAnchor, constant: 7).isActive = true
        
viewController?.view.widthAnchor.constraint(equalTo: self.contentView.widthAnchor).isActive = true

Note, above, that you define a top bottom and width -- there is no LEFT-TO-RIGHT indication where the viewController.view should start on the horizontal path. This can result in some VERY weird behaviors.

Yet, you STILL don't modify the contentView.contentSize to be any bigger than the scrollView size.

To make something scroll, you need the contentView.contentSize to be bigger than the frame.size.

impression7vx
  • 1,728
  • 1
  • 20
  • 50
  • I added `scrollView.contentSize = CGSize(width:self.view.frame.size.width, height: 1000)` just to test things out which made it scrollable but after adding leading and trailing anchors to the viewController.view, it still doesn't show up. – DDavis25 Feb 08 '21 at 22:15
  • You have 2 different problems going on here. To which are completely unrelated. The first being your contentsize = scrollview size. Your next being you need to look up how to add a viewcontroller view as a subview. https://stackoverflow.com/questions/27276561/adding-a-view-controller-as-a-subview-in-another-view-controller – impression7vx Feb 08 '21 at 23:06