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.