Recently, i've been building and app which requires a scrollview, for all the content to fit. I've had a few difficulties creating my scrollview. I've tried a few different methods for building it, but ended up building it in storyboard with autolayout. I've set the height of the view controller to 1000, and therefore also the simulated size to freeform. The width of the content view inside the scrollview, I've to the same as the view which contains the scrollview.
The hierarchy goes like this:
-View controller scene
-Top layout guide
-Bottom layout guide
-View
-Scroll view
-Content view
-Label with text here
-Constraints
-And so on
I've been replicating the process in this link, to create the scrollview:
https://www.ralfebert.de/snippets/ios/auto-layout-recipes/uiscrollview-storyboard/
When running the application on the simulator, I get the following result:
The scrollview is not working properly on the iPad. It seems like the scrollview doesn't adapt to the changes in the width, which happens when its ran on an iPad, since it's bigger. When the app is ran on the iPad, the scrollview stays the same width as if it were shown on the iPhone.
This is how it looks in the editor. The view controller on the left is the view controller which has the scrollview implemented, and the one on the right doesn't. The first image shows the storyboard, when I view it with the iPhone 8, and the second image is when I view it with an 9,7" iPad:
It would be greatly appreciated if anyone could give me a hint to whats going on!
EDIT:
Left side is what it looks like, and right side is what it should look like:
Add the UIScrollView to your main view
Set Top, Left, Right, Bottom constraints all to Zero
Add a UILabel inside the scroll view
Set number of lines to Zero
Set Top, Left, Right, Bottom constraints all to Zero
Set the label's width constraint equal to the scroll view's width
That should do it :)
This will keep the label's text aligned to the top of the scroll view. If you add enough text to fill more than the full screen, it will scroll vertically.
Related
I need to make something like this
the problem is 6 buttons in the middle, how to make it manually by using scroll view? I need to use scroll view since it will be a problem if in landscape mode
here is what I make
https://drive.google.com/file/d/10TBjW3qAaDEGJ_I_EQBPr5anzGcG1-er/view?usp=sharing
the problem is, in iPhone 5s, it seems it can be scrolled horizontally even though I have set width of content view to be the same as scroll view, and in larger phone, it seems not located centrally and not fill the screen
I don't know what went wrong with the auto layout
I unzip your project, and open the storyboard to check those constraint. You can change device to iPhone SE as the red rectangle marked. I notice that the Width of Content View is fixed with 375. It should be as the same as the width of Scroll View.
After updating the width of Content View constraint, I remove the Width:375 constraint, and apply new one that to equal to the width of Scroll View. It looks like the following image.
Hope those help you.
I am using autolayout in Xcode and am creating a fairly tall (1300px) ViewController with a uiscrollview to navigate up and down. I made the viewcontroller freeform so its dimensions are [375,1300]. I then incorporated the scrollview, added the content view as well as all the subviews & constrained everything, leaving no constraint errors. At runtime there are no errors, but everything is all smushed into the normal screen size when I intended for it to be very tall (~1300px) and scrollable. Any clue as to what I am doing wrong?
Below is a diagram showing the issue where:
The Outer Black Blox is the ViewController's view
The Inner Black Boxes are subviews
and The Blue Box is what is displayed on the screen
Yes, your correct #Ryan Cocuzzo. I think you need to set sub view priority from 1000 to 250. See below screen shots.
1) Select height constraint of the subview
2)Then go to show to size inspector
3) Now change the priority constraint from 1000 to 250.
4)Finally you get like this
Make sure the following are true:
The 4 content views are subviews of the UIScrollView, not your view controller.
The autolayout constraints on the 4 content views must reference the UIScrollView only, not the view controller's view.
The 4th box cannot have a bottom constraint.
It looks like the 4th box has a constraint to make it's bottom align with the bottom of the view controller's view. This would force the other views to smush up together. The 4th box also cannot have a bottom constraint so that the scrollview can resize itself to encompass it's subviews.
I'm experiencing some trouble managing a scroll view embed inside a navigation view controller. I'm using Auto Layout and I'll try to explain the problem the best I can.
I embed a scroll view in the controller's main view and pinned the top, left, bottom and right borders to main view's borders.
I embed a standard UIView inside the scroll view and gave it a fixed height of 800 points. This should act as a container for all my controls (let's name it content view). Then again I pinned its top, left, bottom and right borders to the scroll view's borders. Because the bottom space constraint of scroll view's descendant view was negative, I adjusted it bringing it back to 0.
For the width, I CTRL-dragged from the content view to the main view and added a Equal widths constraint.
Finally, I added an image view and placed it at the top center of my content view adding some further trivial constraint.
The storyboard for the situation I've just depicted is shown above (in the document outline you should see all the constraints I've defined).
My scroll view works, it scrolls fine and the image view is well-centered where it's supposed to be. However, there's a gap between the end of the navigation bar and the start of the scroll view and I can't figure out why. It seems to be as high as the navigation bar, but I have no idea about how to fix it. The image shown below should make you understand what I'm talking about (the content view is highlighted in grey for clarity).
I didn't write a single line of code to achieve this result. I would really appreciate any kind of help and I'm ready to give you all the information you need to help me address the problem.
I think you should uncheck Adjust Scrollview Insets property of your viewcontroller to get rid of this....
you have to uncheck Adjust Scrollview Insets
Ok, I tried the page with several buttons but all of them will not fit in one screen. So, I create the scroll view and the view and then put all of buttons inside the view. The storyboard look fine. Other devices such as iPhone 5 and 5S look fine but when I tried 6 and 6 plus, the size of scroll view was incorrect. I couldn't figure what went wrong.
You have to modify constraint for
scrollview with uncheck constraint to margin with all left right
bottom and top is zero
Container view to super view set equel width constraint
container view to scrollview set horizontal center constraint.
Each UIBUtton set top/left/bottom with constant height not set bottom constraint.
I'm working on application for iOS. Trying to use Autolayout with Scroll View.
But there is some problem. I have View -> Scroll View -> Content View hierarchy. I pinned (top, bottom, left, right) of the Scroll View to the Main View, and also pinned Content View to Scroll View.
Then i've added imageView on the top of the screen and text label below, to the Content View. Added them constraints including vertical spacing (8pt) from image to label. In Preview everything looks ok and label placed right below image, however when i'm running the app on iOS Simulator, my label places right in the middle of image. Please, any help?
We found an answer in the comments: it was due to a wrong content scaling mode of the image. It needed to be ClipToBounds.