0

I have got 2 view controllers. First one is the main one with just once button on the screen. When clicked on that button I have my second view controller showed in the bottom of the screen.

My second view controller has got scrollview as a subview. Although I set delegate of the scrollview it doesn't respond to scrolling.

Similar issue to mine usually resolved once delegate added, but not mine. Any idea what I missed here?

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        let button = UIButton(frame: CGRect(x: 100, y: 100, width: 60, height: 30))
        button.backgroundColor = .black
        button.setTitle("Button", for: .normal)

        button.addTarget(self, action: #selector(self.touchUpInside), for: .touchUpInside)

        self.view.addSubview(button)
    }

    @objc func touchUpInside() {
        print("Button Clicked")
        let vc = SecondViewController()
        present(vc, animated: true, completion: nil)
    }

}

SecondViewController with the scrollView

class SecondViewController: UIViewController, UIScrollViewDelegate {

var scrollView: UIScrollView!

init() {
    super.init(nibName: nil, bundle: nil)
    modalPresentationStyle = .custom
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = .clear
    let displayWidth: CGFloat = self.view.frame.width
    let displayHeight: CGFloat = self.view.frame.height

    self.scrollView = UIScrollView(frame: CGRect(x: 0, y: displayHeight - displayHeight / 3, width: displayWidth, height: displayHeight))
    self.scrollView.contentSize = CGSize(width: displayWidth, height: displayHeight)
    self.scrollView.backgroundColor = UIColor.blue
    self.scrollView.isPagingEnabled = false
    self.scrollView.isScrollEnabled = true
    self.scrollView.showsHorizontalScrollIndicator = false
    self.scrollView.showsVerticalScrollIndicator = false
    self.scrollView.isUserInteractionEnabled = true
    self.scrollView.delegate = self

    self.view.addSubview(self.scrollView)
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print("scrollViewDidScroll()")

}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    print("scrollViewWillBeginDragging()")
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
    print("scrollViewDidEndDecelerating() ")
}

}

scrollView methods never get called.

YuryTom
  • 75
  • 8

2 Answers2

0

It might be because your content size is the same as your scrollview frame - since the content size is the same as the frame, there is no reason for it to scroll. Try increasing the height (for vertical scrolling) or width (for horizontal scrolling) and see if it works then

  • Hi Mikhail, yes, you are right. Do you know how to handle touches on the scroll view in that case? As I commented here already, I want to be able to move the view up. It should be similar as when you click in the Apple Maps on a pin and see a popup in the bottom of the screen. – YuryTom Feb 28 '19 at 21:09
  • In that case, use a tap gesture recognizer. Also, make sure you set the cancelsTouchesInView property of the tap gesture recognizer set to false before you add it to the scrollview else it won't get detected according to this post https://stackoverflow.com/q/16882737/6021794 – Mikhail Bhuta Mar 02 '19 at 02:55
0

Problem is with these two lines of code

self.scrollView = UIScrollView(frame: CGRect(x: 0, y: displayHeight - displayHeight / 3, width: displayWidth, height: displayHeight))
self.scrollView.contentSize = CGSize(width: displayWidth, height: displayHeight)

Either the scrollView.frame.size should be smaller or the contentSize should be larger. If both are same, the scrollView has no need to scroll.

If only the heights of frame and content size are different, you'll get vertical scroll. if it is the widths, you get horizontal scroll.

Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
  • Hi Nitin, yes, it solves the issue. My trouble here is that I still want to handle touches on UIScrollView even if the content size is the same as frame size. I just want to move the view when user scrolling up, and when my view is full screen, user will scroll the content if it is larger than the screen. I hope it makes sense. It is something like in Apple Maps, when you click on a point on the map, you get a popup from the bottom. – YuryTom Feb 28 '19 at 20:58
  • @yuryTom my approach for that requirement would be to either 1) add a tap/longpress gesture recognizer, capture events or 2) just add a button to the whole scroll view, while removing them when the scrollView is movable. I'm not sure if that'd solve it – Nitin Alabur Mar 01 '19 at 14:34
  • thanks, I will have a look. I might use a table view for that. Anyway, the scrolling issue was due to content size. Thanks again for your help. – YuryTom Mar 01 '19 at 19:50