Vertical space constraint strange behavior - ios

I am trying to implement UIScrollView from Storyboard using Interface Builder. The ViewController im working on have UINavigationBar.
This is the steps i perform within my Storyboard
adding UIScrollView and set the top, bottom, left, right spacing to 0 (not to margins).
add UIView and set it to be a child of the scrollView. set its spacing to: top, left, right to 0 and it width and hight to the value listed, also set spacing to bottom of the viewcontroller.
The strange behavior is that the top constraint get way high than expected,
What am i missing here?
Thanks!

Well what you can do is select your view controller from the heirarchy on the left and in the attributes inspector uncheck "adjust scroll view insets".

Related

I need to scroll a floating(fixed) UIView in my storyboard after a specific point in UIScrollView

In my storyboard, I have a parent UIScrollView in which I have a UIView of height about 2000px.
I have designed the whole view & placed an another UIView at bottom of parent scroll view so that it will be fixed in the UI. But at the specific point, I need to place that UIView(i.e.fixed) to get scrolled. Like after I scroll a UILabel, I want to scroll that UIView below that & get disappeared from the bottom.
I know its hard to explain but I am stuck at it for so long.
P.S. I'm using AutoLayout.
In this case it is probably best to have the view outside the scroll view but connect constraints to the inner view as well. You use priorities to control those constraints.
In your case you would decrease your bottom constraint priority (to 500 for instance). And you would add a constraint like outsideView.top <= insideView.bottom. So connect the outside view to one of the views inside the scroll view and create the relation be smaller (or greater if the 2 items are reversed) or equal to that view point.
To create a demo of this start a new project and open Main.storyboard:
Add a scroll view with constraints to leading, trailing, top, bottom
Add a blank UIView (now called innerView) inside the scroll view and add leading, trailing, top, bottom constraints
Add a height constraint to innerView to 2000
Add an equal width constraint between innerView and your scroll view
Set bottom constraint between innerView and scroll view to 100
Set a distinct color to innerView to see the effect
All other constraints should be set to 0
Add another UIView on the top level (now called outerView)
Pin outer view to leading, trailing and bottom. Also add a height constraint to 50
Set a distinct color to outerView to see the effect.
By now you should have a scrollable scroll view and a fixed view at the bottom if you run the app.
Now connect outerView with innerView and select "vertical spacing"
Double click the newly created constraint and modify it:
(If needed) Reverse first and second item so that the outer view is first item
Change relation to "less then or equal"
(If needed) select outerView.top
(If needed) select innerView.bottom
Set constant to zero
Select outerView and find bottom constraint to safe area (or superview) and set its priority to 500.
Now run the app. If all is correct your fixed view will be fixed until you scroll down far enough. After 2000px is visible the fixed view will be scrolling with scroll view.
You can naturally pin it to any view in scroll view to get any possible effect you need.
I hope this is descriptive enough. Good luck.

Creating fixed subviews outside of uiscrollview

I'm attempting to put a generic view above and a tab bar underneath a scroll view, but can't figure out how to do it. Can someone explain how to properly place these constraints? Ideally I would like a solution non-programmatically, i.e. one that uses the storyboard. Thanks!
I've applied no constraints so far.
This is the desired output:
Well, In a UITabbarVC
Add UIView constraint top, leading and trailing to the self.view and assign height constraint to about 30% of self.view.height
Add UIScrollView add constraint bottom, leading, trailing to self.view and top constraint to bottom of the added UIView
First you have to give leading, trailing, top and a height constraint to your top view like so:
Then you need to give your scrollview, top, leading, trailing and bottom constraints like this:
And finally you give your tabbar leading, trailing and bottom constraints as shown:
First of all you need three things
UITabBarController
UIView
UIScrollView
Now, Firstly, you need to go in your .storyboard file and drag and drop a UITabBarController from the left hand panel
NOTE: If you are using XCode 10 , then that panel is renamed as Library which is available at the top bar with a Apple Home Button type button
Now, when you dragged UITabBarController, you will get some View Controllers attached with it by default.
Drag and drop a UIView to one of the view controller( preferably the first one) and set some Auto Layout constraints by selecting that View and clicking on the Add New Constraints button in the bottom bar ( where your Current zoom size is available) . You can add the following constraints :
Leading (aka left) = 0
Top = 0
Trailing (aka right) = 0
You also need to provide a height .
If you want to provide a constant height, then you can provide it using the same Add New Constraints option
However, if you want to provide a variable height like in Percentage , then hold Control on keyboard and click and drag from View that you dragged previously to the View Controller's View . Select the option Equals Height. Now it will take the height of the complete View Controller as it is taking 100% of the height of View Controller by default and we need to change that.
To change that, click on the View again , click Hide/Show inspectors (available on the top right corner) and select Size Inspector . There will be a constraint named Height which will be shown equal to superview . Click on the Edit button next to it and change the value of text field named Multiplier to the percent you want (make sure the value of constant text field is 0). If you want the height to be 20%, then input 0.2 and so on.
Next, You need to drag and drop a UIScrollView just below the custom view (that we just created and added the constraint to). Now set the constraints for the Scroll View in the same way you did for View.
For the Scroll View, you need to set the following constraints :
Leading (aka left) = 0
Top = 0
Trailing (aka right) = 0
Bottom = 0
and hit the Add Constraints button.
Now you will get the desired output . The only thing that will remain will be changing the colour of the View and changing the text label and icons for the UITabBarController items. You can change that via Storyboard easily.
Do let me know if you need any further assistance!

Autolayout with UIScrollView

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.

why does my view embedded in UIScrollView scroll only a little?

In my UIViewController I put a UIScrollView and attached constraints:
Then I added a view to this scroll view with following constraints:
and positioned it like this:
so as you can see it is quite long. When I run the app I can scroll it just for couple pixels instead of completely to the top of the screen. I want to make it possible to scroll it to the top so that it can cover everything what's under it. So what am I doing wrong here?
========= EDIT
for clarification - I want to achieve an effect that when user opens this screen, 10% of the screen is covered by the view. User can slide up this view and then it covers 90% of the view. And he can slide it down back to the 10%. Can you help me with adjusting constraints so that it looks good on every screen size?
Take a UIView inside the UIScrollView.
Set the constraint for scroll view as leading, trailing, top, bottom.
Select UIViewController in the story board and click size inspector then choose freeform then set the view height as bigger.
Then set constraint of UIView is leading , trailing , top , bottom as 0, so that the UIView will be same as ScrollView region. Now set height constraint something bigger so that you can design image view, label 1 and label 2. Then set constraint like the image view : top to UIView, leading , trailing and height.
For label 1: leading, trailing , vertical to UIImageView, height.
For label 2: leading, trailing , vertical to label 1, height and bottom to UIView (Important as based on this content view will be set).
After setting constraint You can select UIViewControl from story board and set the SizeInspector as fixed as it was before.
It will show the screen as you need (Image view, below label 1 and on scrolling label 2)
I have tested it in my demo project.
Hope it helps

Using autolayout constraints to define height minus a constant with storyboard

Is it possible to have a View with autolayout , such that the subview has equal height with parent minus a constant?
The Hierarchy is UIView > UIScrollVIew > UITableView. The view is also embedded in a UINavigationController
Yes. You simply pin the subview to the top and bottom of its super view with the constant set to the offset you wish.
As the outside view changes, the inner view will change to match and maintain the offset. You will need to set left and right constraints as well using the same mechanism.
Edit after discussion:
The UIScrollView needs to know the size of your content. You can not simply use auto layout without a few adjustments.
Assumption is your view layout is:
UIViewController -> UIScrollView -> UIView -> UITableView
For your UIView, pin the top, bottom, left and right to the UIScrollView.
To see things properly in storyboard you also seem to have to center the UIView horizontally and vertically. Once you add this you should see the content fill the scroll view bar any edge offsets you set in your pinning.
Pin your UITable to the UIView top, bottom, left and right.
You are going to have to adjust the width in your code somehow to the size you want to scroll horizontally.
In Interface Builder:
Clear constraints on your subview
Select your subview
Choose Pin in right bottom corner of IB view
Select bottom pin and enter your constant (I entered 30), select top pin and enter 0.
Select other constraints if needed.
Accept with button Add x constraints

Resources