so I have added scrollview to my screen.
However, I'm unable to scroll up and down.
My scrollview width and size are 414 and 784 respectively while my content size is scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 800)
Why can't I scroll up and down?
How do I fix this?
You can add a UIView (call it content view) inside your scrollView with top, botttom, left, right constraints to zero. And give height constraint = 800 for this view.
This should work!
Basically, you need to add some content to the scrollView. If the height of contents becomes greater than the scrollView height, the scrollView becomes scrollable.
Related
I have subview in a scrollview and after I remove some subview from the bottom there is a blank space and the scroll view still keeps height, not resizing. How to fix this?
Scrollview's contentHeight & contentWidth is calculated from its subviews(if used auto layout). If autolayout is not used then you need to set its content size.
So whenever you are adding or removing subviews in scrollview you need to take care of its contentHeight and contentWidth.
If you're using auto layout, you need to add constraints to newly added view in such a way that scroll view can calculate its contentHeight from it.(in auto layouts you don't need to explicitly set the content height to scroll view)
If you're using frame base layout, you need to set the contentHeight explicitly to the scroll view. In frame base layout, you need to take care of scrollviews content size.
Why is there blank space in scrollview?
When a subview is removed from scrollview, its content size need to be adjusted. It means it is not able to calculate its content size.
you can use this line
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: (YOURVIEW) - (THEREMOVEDVIEW))
I recommend you that you must save the height before remove view for later you can adjust.
UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself like below:
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 700)
note: change width and height as you want.
you can try this also:
let content: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = content.size
This is my setup:
I do not know what I am doing wrong. The image view is bigger than the size of the view and of the scroll view. The constrains are set al followed:
Scroll view: equal heights to View * 0,5, equal width to View, center Y and X to View.
View (inside Scroll view): pinned all zero's inside Scroll view, equal heights and width. I also tried instead of equal heights and widths to center X and Y inside Scroll view, but it won't scroll.
How can I let the Scroll view scroll? Thank you.
Add a leading, trailing and top constraint and equal height of UIScrollView to superview with 0.5 multiplier. Now to your contentView (the UIScrollView subview), add a leading, trailing , top and bottom constraint. Also add equal height and width to UIScrollView. Set the height to a priority of 250. Add constraints for UIImageView inside this contentView.
Since the contentView will have a fixed height of low priority equal to the UIScrollView height. This fixed height constraint will break once the UIImageView total height(based on the constraints you add) will get larger than the UIScrollView height and the content will become scrollable. So at the very least you will always have a view half the screen size and become scrollable once the content becomes too large vertically.
You need to give contentSize to scrollview.
ScrollView.contentSize = CGSize(width: 1000, height: 500)
Which constrains have you given to imageview?
set constraint of imageview:
Trailing ,leading,top,bottom - 0 and also give height constraint.
I am facing a trouble with UIScrollView using auto layouts. I have the following view hierarchy with constraints.
ViewA (leading,trailing,top and bottom spaces to superview).
-- ViewB (leading,trailing,top and bottom spaces to superview).
--- UIScrollView (leading,trailing,top and bottom spaces to superview).
Here ViewB is adding on ViewA and i have some textfields,buttons and UITableview these all are placed inside UIScrollView.This is my view hierarchy.
Now i want to my Scrollview height increase dynamically based on tableview number of rows.
For this I'm using bellow code but it not working.Can any one please suggest me.
CGFloat height = MIN(self.view.bounds.size.height, placeTbl.contentSize.height);
self.Height_PlaceTbl.constant = height;
[self.view layoutIfNeeded];
if you want to increase your ScrollView height based on tableview number of rows you can try this
first get number of your tableview rows and then get height and width of your scrollview
let width = self.scrollview.layer.bounds.width
let height = self.scrollview.layer.bounds.height
after this , you can increase height of your scrollview like this
self.scrollview.frame = CGRectMake(0, 0, width, height + numberofrows)
Black - screen frame/size
Red - default UIScrollView's frame/size
Yellow - my button.
I want to always keep that button at the bottom. So e.g. on 3.5in screen, scrolling is available and button is at the bottom, no problem. Now, when we move to 5.5in screen, it becomes like in this image, button is not at the bottom anymore. What I am trying to do is change UIScrollView's height, but it does not work:
if(self.view.frame.height > contentView.frame.height)
{
print("fixing scroll view")
contentView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 2)
scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height * 2)
}
self.view.frame.height * 2 is just for testing. What could be wrong?
Late response, but for those who need a button to stay at the bottom of the screen in a scrollview, if the content view should be the same as the screen height, try this:
Set ScrollView top, left, right and bottom constraints to 0 from parent
Set ContentView to Equal Width and Equal Height of ScrollView
Set ContentView top, left, right and bottom constraints to 0 from ScrollView
Give each element inside the scroll view some constraints, linking everything from top to bottom
Select one constraint between the elements and remove it, or make it low priority. I'd say to make it low priority because if you have to delete the Equal Height, the layout won't break.
With auto layout all you need to do is add pin constraints to your scrollView to each corner of the superview.
Set your 4 constraints to 0 and you'll be all set.
I have a UIStackView,inside a UIScrollView to show dynamically added subviews horizontally arranged. Current solution will start displaying items from the left, I would like to start distributing items from the centre without changing the width and height of the subviews. How do I do that? Im also open to solutions which doesn't use UIStackView as well. That way I could support devices < iOS9.
(Current)
(Expected)
Short answer :
ScrollView constraints
Leading >= 0, Trailing >= 0, Top >= 0, Bottom >= 0
Center X and Center Y
StackView constraints
Leading = 0, Trailing = 0, Top = 0, Bottom = 0
StackView width = ScrollView width (priority low :250)
StackView height = ScrollView height
Long answer
Firstly, your structure is good, we have :
UIScrollView
UIStackView (horizontal)
Subviews
So, to reach the goal we should :
Center the UIScrollView
Set the contentSize of the UIScrollView equal to the intrinsic content
size of the UIStackView
Here is how to do it :
Step 1: Center the frame of the UIScrollView
CenterY and CenterX constraints used to center the frame of the UIScrollView
Leading Space >= 0, Trailling Space >= 0, Top Space >= 0, Bottom Space >= 0 are used to prevent the frame of the UIScrollView to exceed the frame of the parent view
I used placeholder intrinsic size to don't show errors related to the contentSize of the UIScrollView (because we don't have yet the subviews so the contentSize).
Now, the frame of our UIScrollView is Ok, go to the next step :)
Step 2: add the horizontal UIStackView
Top, Bottom, Leading, Trailing constraints are used to fix
UIStackView frame
Equal Height and equal width used to calculate the
contentSize of the UIScrollView
PS. Any change in the frame of the UIStackView, change the contentSize of the UIScrollView
Step 3: add subviews
Because we use Fill Distribution in the UIStackView all subviews must have a intrinsic content size (or height and width constraints (not preferred)).
For example, if we use Fill Equally, only one subview with intrinsic content size (or height and width constraints (not preferred)) sufficient, the other subviews size will be equal to this one.
For Example: I will add 3 labels (please remove the placeholder intrinsic size of the UIScrollView)
It works !! no, no, not yet try to add fourth and five labels :)
Why ?
To understand we will calculate the frame of each element of the view with two examples :
The parent view size : 200, 200
The first label intrinsic content size : 120, 50
the second label intrinsic content size : 150, 50
First example (only the first label in the UIStackView)
UIStackView intrinsic content size = 120, 50
UIScrollView contentSize = 120, 50
UIScrollView frame = 40, 75, 120, 50
All frames are OK
Second example (with the two labels)
UIScrollView frame = 0, 0, 200, 50
UIScrollView contentSize = 200, 50
UIStackView intrinsic content size = 200, 50
So, the UIStackView can't show correctly the two labels (because the width of UIStackView lower than the two labels width), and we don't have the scroll because, the UIStackView constraint width is equal to UIScrollView width's.
It works when the UIStackView intrinsic content size is lower than the max UIScrollView frame.
To Fix that, we change the priority of the width constraint to a value lower than the UIStackView intrinsic content size priority value, and all works fine :)
Hope that helps.
Code source