In my application, I created a scrollView and set the width of this scrollView to twice the width of the screen. Then I created two contentViews. (contentView1 and contentView2) I use these contentViews as two screens. When I click on the "YENİ KAYIT" button in contentView1, the scrollView moves forward and contentView2 comes. The problem with contentView2 is that if I click anywhere other than the Stop button, it goes to an irrelevant place.
You can click here for detailed video : https://github.com/krmdmr7/ProgrammaticScrollView/assets/120031527/a873700b-9bd9-466c-b9b4-8670a1e8604a
You can click here for detailed code : https://github.com/krmdmr7/ProgrammaticScrollView
My UI Views :
private var allRecordsScrollView: UIScrollView = {
let allRecordsScrollView = UIScrollView()
allRecordsScrollView.isPagingEnabled = true
allRecordsScrollView.contentInsetAdjustmentBehavior = .never
allRecordsScrollView.showsHorizontalScrollIndicator = false
allRecordsScrollView.translatesAutoresizingMaskIntoConstraints = false
allRecordsScrollView.contentSize = CGSize(width: UIScreen.main.bounds.width * 2, height: UIScreen.main.bounds.height)
return allRecordsScrollView
}()
private var contentView1: UIView = {
let contentView1 = UIView()
contentView1.translatesAutoresizingMaskIntoConstraints = false
contentView1.backgroundColor = UIColor(named: "LightGreen")
return contentView1
}()
private var contentView2: UIView = {
let contentView2 = UIView()
contentView2.translatesAutoresizingMaskIntoConstraints = false
contentView2.backgroundColor = UIColor(named: "Green")
contentView2.isHidden = true
return contentView2
}()
Life Cycle :
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
configureUI()
}
UI Görünümleri Kısıtlamaları :
private func configureUI(){
view.addSubview(allRecordsScrollView)
NSLayoutConstraint.activate([
allRecordsScrollView.topAnchor.constraint(equalTo: view.topAnchor),
allRecordsScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
allRecordsScrollView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2),
allRecordsScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// ContentViews constraints for allRecordsScrollView
allRecordsScrollView.addSubview(contentView1)
allRecordsScrollView.addSubview(contentView2)
NSLayoutConstraint.activate([
// contentView1 constraints for scrollView
contentView1.topAnchor.constraint(equalTo: allRecordsScrollView.topAnchor),
contentView1.bottomAnchor.constraint(equalTo: allRecordsScrollView.bottomAnchor),
contentView1.leadingAnchor.constraint(equalTo: allRecordsScrollView.leadingAnchor, constant: 0),
contentView1.widthAnchor.constraint(equalTo: allRecordsScrollView.widthAnchor, multiplier: 0.5),
contentView1.heightAnchor.constraint(equalTo: allRecordsScrollView.heightAnchor),
// contentView2 constraints for scrollView
contentView2.topAnchor.constraint(equalTo: allRecordsScrollView.topAnchor),
contentView2.bottomAnchor.constraint(equalTo: allRecordsScrollView.bottomAnchor),
contentView2.leadingAnchor.constraint(equalTo: contentView1.trailingAnchor),
contentView2.widthAnchor.constraint(equalTo: allRecordsScrollView.widthAnchor, multiplier: 0.5),
contentView2.heightAnchor.constraint(equalTo: allRecordsScrollView.heightAnchor)
])
}
Button Functions :
@objc func recordButtonTapped() {
print("Record button tapped.")
contentView2.isHidden = false
contentView1.isHidden = true
let xOffset = allRecordsScrollView.frame.width / 2
allRecordsScrollView.setContentOffset(CGPoint(x: xOffset, y: 0), animated: false)
}
@objc func stopRecordButtonTapped(){
contentView2.isHidden = true
contentView1.isHidden = false
print("Stop record button tapped.")
allRecordsScrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}