UIScrollView Causing "Misplaced Views" AutoLayout issues - swift

I'm running into a strange AutoLayout related issue when I use a UIScrollView (the issue does not occur without it).
I have a UIScrollView that is constrained to the boundaries of a UIView (contained within a UIViewController), and within that, I am attempting to place a UILabel and UITextField side by side. I have constrained the UILabel to the left and upper boundaries, with it's width and height constrained (see screenshot below):
Right next to this UILabel is a UITextField, which is constrained to the left, top, and right, as well as having the height constrained. However, this results in a "Misplaced Views" warning, that states "Expected width = 163, Actual width = 413", shown in the screenshot below:
When I choose to "Reset to Suggested Constraints", the "Misplaced Views" issue disappears, but in it's place I am left with a width constraint of 413 points, which is something I'm hoping to avoid, as I would not like this UIViewController to be horizontally scrollable on smaller devices.

A scroll view has a size (the size it takes up on the screen) and a content size (the size of the entire scrollable area). In Auto Layout, the content size is automatically computed from the constraints of the items in the scroll view. This is a problem, because you are trying to make the scroll view have the same width as your screen, and then have the items constrained to that. When you do that, Auto Layout insists that you give your text field an explicit width so that it can calculate the width of your scrollable area.
To do what you want, do the following:
Add a "content view" to your scroll view. This view will be the only top level item in your scroll view. It will hold all of your content as subviews of it. Drag out a UIView and add it to your scroll view. Constrain its top, leading, bottom, and trailing edges to the scroll view. Constrain its width to the width of the scroll view. Give it a height constraint and set it however big you want your content area to be.
Add all of your labels and textfields to this content view. Now you can constrain them centered in your content view or constrained to the edges, and it will work as you want.

Related

Swift: UIScrollView doesn't scroll after the user edits the height of a UITextView

I have two UITextView inside a UIScrollView and the TextView can resize as the user types more text into them. I need the page to be scrollable to account for this so I put them in a ScrollView, however the height of the ContentView doesn't change with the TextViews. Here's what I tried so far:
adding a bottom constraint from the last helper text to the bottom of
the content view with priority 1000 and constant 210. This doesn't
work because the section will be scrollable before the TextViews get
taller and on tablets or larger devices it will make my first
TextView taller.
adding a bottom constraint with a constant of 210 or lower will give me a layout error.
I also thought about calculating the height of the content view and maybe adjusting the constraint based on that but this doesn't work either.
Here is a link to the file in question: https://www.dropbox.com/s/g7q1lbj0pxx14jh/Main.storyboard?dl=0
Any suggestions?
It's also worth noting that the keyboard will cover the second TextView when the first one gets too big so I will eventually need to edit that constraint based on the height of the first TextView (to give it more padding at the bottom so that the user can bring the second TextView in view.
Here is the fixed variant https://www.dropbox.com/s/gujry7ngziaipm7/Main.storyboard?dl=0
I found just 1 error:
You forgot to specify bottom offset from last view in View Container which is UILabel - "Being clear...", so your view container doesn't know when to resize as soon as you do not specified all offsets to it.

StackView not appearing correct on all screen sizes

I have a ViewController that contains a Stack View. In this Stack View there are 3 more Stack Views. The first contains 3 labels, the second contains more Stack Views with buttons and labels, and the third contains 2 buttons and a label. And at the bottom, there is a button. The layout is fine on screen sizes from 4,7" and bigger. But when the screen size is smaller, it cuts out the top Stack View. I have tried to set constraints on leading, trailing, top and bottom in different variations (with and without top and bottom constraints) But I can't make it work properly. I have attached screenshots of the different screen types.
EDIT* Depending on the selection from the previous ViewController, the middle Stack View(the one with the check boxes) can be isHidden When its hidden, the view is correct.
Generally you don't set height on UIStackView. They are dependent on it's content. This will cause your stackview to get out of the screen height. Have you used the view hierarchy? If you want to cover the small sized phones you could put your main stackView inside a scrollView to still be able to see all content.
It's a bit difficult to diagnose what's happening without seeing the constraints you apply to the views, however I'll recommend you some things that may help:
Set the space between views using size classes so you can set the right one depending on the screen size and avoid the overlap with the below views.
In case you are doing it, don't set a height constraint for any stackView.
To align the top StackView I would use not only a top constraint to the top bar (of type greater than or equal) but also a vertical center constraint with a right multiplier (with a high priority). In this way you ensure the top stack view is aligned vertically in proportion to the screen size and with the top constraint you ensure the stackView doesn't hide below the top bar.
Make sure you are hiding the right stackView.

Unity scrollview can't scroll to the end

I have a scrollview.Horizontal and vertical scrollbars are removed because it is ugly and I have space constraint.
When I populate items into content of the scrollview as shown in the image
I can't scroll. The view is moving but doesn't scroll.
When I scroll, just scroll to some extent and scroll back to the original position.
What could be wrong?
Check if you have Vertical checkbox enabled on Scroll Rect component.
On your content object after adding ContentSizeFitter, you need to select either Min Size or Preferred Size to make the scroll view content automatically resize. The unconstrained option doesn't drive the height so your scroll view won't scroll.
Make sure that the Height parameter in the Content object is not equal to zero:
What happens when you press play:
Now let's set Height to something much bigger than zero:
Here is what we get when we press play this time:
(The scroll bar appeared!)
Credit: Unity scrollview can't scroll to the end

Vertically centering UILabel programatically in scrollview that doesn't match the view swift

I have a horizontal scrollview that fills 1/3rd of the view.
I want to put a UILabel in the centre of the scroll view, which I have successfully done with:
let reagentsLocationx = mainscrollview.frame.width/2-100
However, I am struggling to centre the label vertically within the scrollview. When I use similar code for reagentsLocationy it centres the label in the view and not within the scroll view.
How do I centre the label, in Swift, just within the scrollview?
Many thanks.
I think this is because you are using frame and not bounds. I don't believe frame will ever be larger than your screen space, but bounds it equal to the view's actual size. Try basing it on the bounds of the scroll views height.
UIScrollView().contentSize.height might be what you are looking for.
No need of setting sizes in code.
Everything done in the storyboard.
Drag your scrollview into your view controller.
Set top, bottom, leading and trailing constraints from the scrollview to the parent view or safe area.
Drag your text label into the scrollview.
Set your top, bottom, leading and trailing constraints to the scrollview.
IMPORTANT set a second leading and trailing constraint, now from the text label to the parent view (UIView - not scrollview) or safe area.
This makes sure that the text label knows his width and so the scrollview knows it too.

How do I set the right margin of a UITextView?

I have a vertically-scrolling UITextView that fills the width of the screen. I need the text view to have margins (contentInset) of 20 pixels on the left and the right. But for some reason, I can't get the right-hand margin to work, either in Interface Builder or in XCode.
The reason I can't just make the text view narrower is because I am adding a subview which needs to run the full width of the screen. Also, I can't turn subview clipping off, because it plays havoc with a load of other elements on the screen.
Anyone know why the contentInset property is not affecting the right margin?
Thanks!
You could add an intermediate view that can be the superview to your text view and what was previously it's subview.
New View with Frame(0,0,width,height)
->TextView with Frame(20,0,width-20,height)
->Subview with Frame(0,0,width,height)