I've been looking for this for hours and have no luck yet. I want to use two custom views inside a scrollview. The first view have a button as a subview that leads the user to the next page(scrolls down). But the button action it's never fired. If I use the button as a scrollview subview everything works fine, but that's not what I want.
The code for the scrollview view controller:
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let redView = View1()
redView.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scrollView.frame.height)
self.scrollView.addSubview(redView.view!)
let blueView = UIView(frame: CGRect(x: 0, y: self.scrollView.frame.height, width: self.view.frame.width, height: self.scrollView.frame.height))
blueView.backgroundColor = .blue
self.scrollView.addSubview(blueView)
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.scrollView.frame.height * 2)
}
func go(to page: Int) {
let y = CGFloat(page) * self.scrollView.frame.size.height
self.scrollView.setContentOffset(CGPoint(x: 0, y: y), animated: true)
}
}
ScrollView Storyboard Configuration
The code of the View1 Class:
class View1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func didTouchUpInsideMoreInfoButton(_ sender: Any) {
print("test")
}
}
Any ideas are welcome. Thanks in advance.
Turn out that I was using a view controller's view and for some reason, the selectors don't work this way. So, what I did was to create a UIView only and then everything works just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With