iosswiftuiscrollviewxcode7.2

How to scroll my UIViewController content?


I've been trying to search for a swift version of a solution to no avail. I was wondering how I can make my view controller scroll content. Right now it's just static, and there's more content below, which I'm unable to scroll.

class ProfileViewController: UIViewController, UICollectionViewDelegateFlowLayout {
    let postsId = "postsId"
    override func viewDidLoad() {
    super.viewDidLoad()


    view.addSubview(profileContainerView)
    view.addSubview(profileTabCollection)
    view.addSubview(containerForCollectionView)
    view.addSubview(scrollView)

    scrollView.widthAnchor.constraintEqualToConstant(view.frame.size.width).active = true
    scrollView.heightAnchor.constraintEqualToConstant(view.frame.size.height).active = true
    scrollView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
    scrollView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true

    scrollView.userInteractionEnabled = true
    scrollView.scrollEnabled = true
    scrollView.delegate = self


    containerForCollectionView.widthAnchor.constraintEqualToConstant(scrollView.frame.size.width).active = true
    containerForCollectionView.heightAnchor.constraintEqualToConstant(scrollView.frame.size.height).active = true
    containerForCollectionView.topAnchor.constraintEqualToAnchor(profileTabCollection.bottomAnchor).active = true

    profileContainerView.widthAnchor.constraintEqualToConstant(scrollView.frame.size.width).active = true
    profileContainerView.heightAnchor.constraintEqualToConstant(250).active = true
    profileContainerView.centerXAnchor.constraintEqualToAnchor(scrollView.centerXAnchor).active = true
    profileContainerView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true

    profileTabCollection.widthAnchor.constraintEqualToAnchor(profileContainerView.widthAnchor).active = true
    profileTabCollection.heightAnchor.constraintEqualToConstant(50).active = true
    profileTabCollection.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
    profileTabCollection.topAnchor.constraintEqualToAnchor(profileContainerView.bottomAnchor).active = true

}

lazy var scrollView: UIScrollView = {
    let sv = UIScrollView(frame: CGRectMake(0,0,1024,768))
    sv.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 3)
    sv.translatesAutoresizingMaskIntoConstraints = false
    return sv
}()


let containerForCollectionView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.purpleColor()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

Solution

  • There are numerous reasons why this won't work. Since you don't provide enough information to be sure what it is, i'd recommend reading this (especially Duncan C's answer) carefully.

    how do i alter the position of a UIImage inside a imageView in SWIFT