0

I am trying out the scroll view in my learning iOS. I added one main view, in that i added few sub views. Then i embedded that main view to scroll view. I also tried to set content off set value in my view did load. But scroll view is not scrolling.

Then i added through programmatically, but my main view was not coming in center. Still i can move my main view. Which i dont want. I want my view to scroll , i dont want any scale or move left or right.

Here is my code : Link

Any solution or help would be great. Not sure why i was not able to do. I can do with table view or collection view. But i want to do in my main view.

Thanks

Samarat
  • 33
  • 5
  • Please review [ask] ... and by the way, your "Project link" does not give access to your code. – DonMag Aug 24 '20 at 17:59
  • @DonMag. i updated my project link – Samarat Aug 24 '20 at 18:08
  • You're doing a lot of things wrong. I strongly suggest you go to google (or your favorite search engine) and search for `UIScrollView tutorial`. Go through several tutorials until you understand how scroll views work and how to set them up. – DonMag Aug 24 '20 at 20:47

2 Answers2

0

I have found multiple issues in your codes and I have done some tweaking and the scroll view is working perfectly. Okay, let's talk what I did :

I did it through storyboard so,

  1. I commented your code in viewDidLoad.
  2. In main.storyboard, I added the scrollview just below your header view and gave a constraint 0,0,0,0 (top, leading, trailing and bottom)
  3. Now I dragged your main view inside the scrollview and gave a constraint 0,0,0,0 (top, leading, trailing and bottom) and equal width constraint to the scrollview.
  4. I added vertical stackview with distribution fill inside the mainview and gave a constraint 0,0,0,0 (top, leading, trailing and bottom)

Check if this is working or not!

sudayn
  • 1,169
  • 11
  • 14
  • i tried, but still not able to scroll the view. If it working, can you please send me the project code ? – Samarat Aug 25 '20 at 18:54
  • when i added scroll view, and when i give (top, leading, trailing and bottom) (0,0,0,0) ist showing some constraints error Need constraints for y position or height – Samarat Aug 25 '20 at 19:01
  • Okay sure! Here is the link : https://drive.google.com/file/d/1Bhe6RPE-9_EPBm1NMd6c-WPKJdlpnDnv/view?usp=sharing – sudayn Aug 26 '20 at 08:17
  • you have to add child view to scroll view and set the child view constraint (0,0,0,0) and also add width constraint equal to scrollview width constraint (important) which will solve the constraint error. – sudayn Aug 26 '20 at 08:19
0

There is some issue with the constraints of the scrollview in your application because of that your scrollview is not scrolling. To add scrollview properly to the exiting view you should follow this link:-

Add a ScrollView to existing View

Or if you want it for specifically for swift 5 then follow this link:-

How to add Scroll View in Swift 5?

Apps Maven
  • 1,314
  • 1
  • 4
  • 17