top of page

ScrollViews, AutoLayout, dynamic content, oh my!

Writer: Ashley Ng
Ashley Ng
Sep 9, 2019
2 min read

Have you ever wanted to display a lot of content in a ScrollView? Have you also needed that content to be dynamic? Well you're in the right place! I struggled with this (for longer than I care to admin) a few days ago, but most of the tutorials and StackOverflow questions were either just for images, or not really dynamic content, or they just suggested TextViews (which wasn't what I needed). In short, the trick is that the UI elements in the scroll view need to have a height (or intrinsic height).


1. Add a ScrollView to your ViewController and pin it to the edges of the parent view, you generally want this to be 0 all the way around.

2. Add a UIView (thus referred to as the ContentView) inside the ScrollView and pin it to the edges of the parent view as well. If you are using the Safe Area Layout, make sure the ContentView is pinned to the SuperView and not the Safe Area. Note there is an error because there is no content in our ContentView and the ScrollView is complaining that it needs a Y position or height. Not to worry, we will be adding content to our view and this will go away!


3. If you want a vertical scroll, make the ContentView the same width as the parent UIView (NOT THE SCOLLVIEW). If you want to have a horizontal scroll, you make it the same height instead. In the my example app for the horizontal scroll, I did the same height as the Safe Area because of the added TabView at the bottom.

4. Add the UI elements you want into the ContentView. In my case, I add a UILabel, UIImageView, and a UIButton. Make sure if you want to display a lot of content in the UILabel that you set `Lines` to 0 so that it can display as many lines of text as it needs.


5. Pin those UI elements however you like to the edges and however you want the spacing to be between them. Don't currently worry about any of the element's height.


6. Now if you added the same elements as I did, you should still have an error that the ScrollView needs a Y position or height. This is because the ImageView doesn't have an intrinsic height. If you removed it (and redo the constraints so that the label and button have a constraint between one another), you shouldn't see the error any more. But I go ahead and set the ImageView height to 256 and aspect ratio of 1:1.

7. Final step is to actually hook up all those elements to show some data. I'm not going to go step by step for this since it's not the topic of the post, but you can look at the GitHub repository for the project if you need help.



This should be a good starting off point for your own app if you're needing something similar (or if it's exactly what you needed, lucky you!).



Happy Coding. Happy Growing.


69 Comments


lindaparker608
3 days ago

Dobrze zaprojektowana edukacja zdalna może być równie skuteczna jak tradycyjna nauka, pod warunkiem że materiały są odpowiednio uporządkowane i angażujące. Krótkie lekcje, ćwiczenia, testy oraz materiały dodatkowe pomagają utrzymać regularność nauki. Ważne jest również umożliwienie uczniom samodzielnego kontrolowania postępów. Strona poświęcona metodom nauki online mogłaby być pomocna zarówno dla uczniów, jak i nauczycieli przygotowujących własne kursy.

Like

briansmith47088
3 days ago

Współczesne technologie informatyczne rozwijają się bardzo szybko, dlatego materiały edukacyjne powinny koncentrować się nie tylko na konkretnych narzędziach, ale także na podstawowych zasadach działania systemów. Artykuły o programowaniu, sieciach, bazach danych, bezpieczeństwie oraz chmurze mogą tworzyć solidną bazę wiedzy. Szczególnie wartościowe są poradniki zawierające praktyczne przykłady, dzięki którym czytelnik może samodzielnie przetestować omawiane rozwiązania.

Like

michellescott012
3 days ago

Profesjonalne szkolenie pilotów wymaga połączenia wiedzy teoretycznej, praktyki oraz bardzo dobrego przygotowania do sytuacji nietypowych. Materiały edukacyjne dotyczące aerodynamiki, meteorologii, nawigacji oraz procedur lotniczych mogą być interesujące również dla osób, które dopiero rozważają rozpoczęcie nauki. Warto byłoby stworzyć przejrzystą bazę wiedzy przedstawiającą kolejne etapy szkolenia, wymagania oraz podstawowe pojęcia związane z lotnictwem cywilnym.

Like

anthonytorres42253
3 days ago

Tematy związane z logistyka i transport mają duże znaczenie dla współczesnego biznesu, szczególnie przy rosnącym znaczeniu handlu internetowego i międzynarodowych łańcuchów dostaw. Edukacyjne artykuły mogą wyjaśniać zagadnienia magazynowania, planowania tras, zarządzania zapasami oraz optymalizacji kosztów. Warto również przedstawiać konkretne przykłady pokazujące, jak technologie i analiza danych pomagają usprawniać procesy logistyczne w małych i dużych przedsiębiorstwach.

Like

charlesharris68869
3 days ago

Rozwijanie kreatywne umiejętności może być przydatne nie tylko w zawodach związanych ze sztuką czy projektowaniem. Kreatywność pomaga również w marketingu, biznesie, edukacji i rozwiązywaniu codziennych problemów. Dobry serwis edukacyjny mógłby publikować ćwiczenia, przykłady projektów oraz praktyczne materiały dotyczące projektowania, pisania, tworzenia treści i pracy z narzędziami cyfrowymi. Szczególnie ciekawe byłoby pokazanie całego procesu od pomysłu do gotowego projektu.

Like
bottom of page