i got an issue with a UIStackView that won't stretch to the superviews bounds.
i've added constraints to the stackView to fill it's superview with a left and right margin. But instead of doing so it resizes it to it's content size. I've set the distribution to .equalSpacing and axis to .horizontal.
In a test project with just the stackView it works fine (despite the fact that in my actual project i'm doing it in code and in the test project in IB).
Here the constraints of the content views get correctly ignored and the stackView takes the space of the superview
i tried changing contentHugging for both contentViews and stackView but nothing changed. Any help is really appreciated :)
i found it myself, problem was that the stackView is inside a UIScrollView to make it scrollable once the stackView extends the screen bounds. As adding constraints to a subview of a scrollView does relate to the contentSize of the scrollView not the frame, the constraints could not be resolved correctly.
I ended up adding constraints to the superview in didMoveToSuperview()
Hopefully this may help someone :)
Related
Ok, so what I have is a UIScrollView that is constrained to all four sides of the main view, centered both vertically and horizontally, and set to have equal width and height to the view. All of the subviews that I put on top of the UIScrollView are showing up when I run the app, exactly where I want them to be, but only the UITextView at the bottom is not. It seems like I've tried every combination of constraints but it never appears when I run the app regardless of what I do. Here is a screenshot of the constraints in the interface builder:
And even when I preview the file Main.storyboard before running it looks like this:
But when I actually run the app, the screen is missing the UITextView, even when I alter the constraints in a number of ways:
Any help with this problem will be greatly appreciated! Thanks in advance.
Avoid putting all your subviews directly inside the scrollView. The autolayout will break apart.
You need to :
Add a UIView inside the UIScrollView, with constraints 0-0-0-0 to leading-top-bottom-trailing to the UIScrollView, and put all your subViews inside that UIView.
After that, you need to set the contentSize of your UIScrollView by code.
Also, you can:
add missing constraints to see what is missing.
The left panel in UIStoryBoard (Document Outline), you can press the red arrow showing up to see what is missing or conflicting.
Check out Apple documentation for more details.
If you want your scrollView only scrolls vertically you shouldn't set its height equal to its superView so remove it and just set the width to its superView and then it should calculate the height based on the subViews inside it
I offer you to drag a UIView in your scrollView and set the constraints to its four sides, and name it containerView , then set its width equal to background view and start laying out your views inside it not inside the scrollview :)
If Height of all views in the scrollView is clear, it can infer the scrollView's height
in this case you can set a fixed height to your containerView like 800 to get rid of the red lines and check how it works :)
I have noticed some clues you might have to solve which could probably solve the issue. Since the scrollView is extend by its content's elements, you have to explicitly deal with each element in the scrollView:
For imageView on the top is not well constrained, you need to give it a width or aspect ratio. Fix the imageView issue might solve textview height problem, even it's not still is a good start.
TextView bottom anchor is equal to scrollView bottom anchor, but you have to know textView is also kind of scrollView. So it's not reasonable to constraint each other at same time. Because both of them don't have explicitly height. You can try to type some words in the textView which will at least give it height by its content, then the scrollView can detect the textView bottomAnchor. You might see something then.
Your scrollView's height is equal to view's height is also weird, scrollView shouldn't constraint its height at first. Because it can't be "Scroll"View anymore because it's height is constant. you should let its width equal to view, and let the height be decided by its element's height. Then it will be literally a scrollview.
Hope it helps
I have a UIScrollView which is getting resized on runtime according to some conditions. Now i used auto layout properly without using a single constraint extra or less. The first subview of scroll view has leading, trailing, top, bottom and equal width to scroll view and the height of the content view is managed by its inner content which is also changing depending on the runtime condition. Now according to my constraints my content view should not scroll horizontally as i used equal width to scrollview. But its happening. Can anyone suggest what am doing wrong? Here is the images of constraints i have used.
NOTE:- I have seen the posts on stack where this kind of question answered but my case is different.
Content UIView width should be equal to the width of UIScrollView's superview for instance, not UIScrollView itself.
In my case, I used to 'constraint to margin' option while I am adding constraint from storyboard. When I only add trailing and leading without relative to margin problem solved. I did not give any superview to my UIScrollView.
It's possible to make an UIView adjust automatically his height to accommodate an StackView, via Interface Builder? I' wanna put a background on a StackView, but as long it is not possible, as StackView is not renderable, I'm using it inside a UIView, but XCode is complaining about a height constraint.
Thanks a lot]1
You need to constraint top, bottom, leading, trailing of the UIView to the UIStackView, and nothing more.
Well, sometimes the answer is so obvious that we can't see it. I've just put a height constraint of the UIView to the height of the StackView and it worked like a charm!
I have a followed this answer Putting a UIScrollview in a viewcontroller (storyboard) to embed a scrollview in a viewcontroller (Mine is vertically instead of horizontally).
All the constrains are working except the fact that the view bounces back when it is let go. I have tried a lot of different solution, but none is working! Please help, I'm using autolayouts my main goal is to be able to show all the buttons on the screen through scrolling.
EDIT:
I have been trying all types of ways to increase the contentSize, but nothing happens - the scrollview won't scroll properly. I tried different codes from people with the same problem, but the bouncing won't stop. Am I doing anything wrong on my constraints? This is extremely frustrating, please help. My constraints (using autolayout) (as I said, I followed this answer Putting a UIScrollview in a viewcontroller (storyboard))
You have to increase your scrollView's contentSize to fit all of it's sub views.
You should add subview to scrollview, then add all other views to that subview.
scrollView
contentView
subview_1
subview_2
Then add constraints between scrollView and contentView (leading, trailing, top, bottom) and contentView should have width and height constraints set properly (set width equal to scrollView width)... the content size will be calculated automatically.... just set height
I have never used UIScrollView before, so I apologize in advanced if my question seems dumb.
I add a UIScrollView to my storyboard scene and then I added labels inside my the scrollview. But when I run my app nothing scrolls. and my text runs off the app screen. Here is a screenshot of the list of items inside my storyboard scene
My question is, is there any steps I may have missed to get this working?
Set this in code:
// the size dimensions should be larger than the scroll view's frame size.
scrollView.contentSize = CGSizeMake(xSize, ySize);
Your problem is because scrollView has contentSize property which is not set yet. So it's not scrolling. To do it in code you should try out David's answer to try it in storyboard, you should add these constraints
These two constraints will make height-
scrollview vertical space to toolbar(bottom constarint)
scollView vertical space to launchImage(top constraint)
These two will make width
scrollView leading space to container/superView
scrollView trailing space to container/superView