I am so confused about UIScrollView.
I'm working with a view contains many other views. And that view placed in a UIScrollView.
How can I change height of this view?
I've tried so many ways but they didn't work. I tried to change frame by CGRect but view's child went wrong.
First of all you should be using constraints using auto layout.If all the constraints are justified then the there will be no warning.At that moment the scrollview will fit all the views under its contents view and mange the content size of it by itself.
If you want to increase any view in the scrollview to increase in height you may increase the height constraint and the scrollview will manage the constentsize by itself.
Related
I'm writing an ios application, which has child views (like fragments or subviews) which are placing in a simple UIView in a UIViewController and the height of UIView is approx 300px. Im just loading subviews in UIView, however every subview has its own content that may not be fit in 300px thus uiscrollview cannot got though out the last view.
My problem is, Im unable to solve the scroll view content in order to scroll from parent UIViewController along whith its child view controller subviews, as the subviews are longer than 300px and thus uiscrollview is unable to get te last element.
I tried to to give static scrollview.contentSize.height = 1000 and hence the scroll view can scroll below the screen but im unable to click on the later views as it seems uiscrollview is unable to read that element.
My question is, how can i assign dynamic uiscrollview height assuming that im using autolayout in my uiviewcontroller and I want to calculate scroll height according to children based in my UIViewController. Im using swift 2.0 and autolayout in storyboard.
Your constraints should be like,
scrollview - top,bottom,leading,trailing
view (content view) - top,bottom,leading,trailing,fix height and horizontally center in container
and add your all other stuff in that view. you will got desired result. it will scroll in small screen size then content view and will not scroll for bigger screen then content view.
second thing you need to increase height of your content view as more subviews add. your content view's height should be equal to all subview's height and spacing
After this setup if you unable to scroll then check content view's bottom constraint's constant in size inspector. make it zero (if unable to scroll then only).
hope this will help :)
I have a real problem with detecting UIScrollView height. I searched a lot and found this solution:
So I dragged from Content View to View and made Equal Height. But I does not work for me.
I also tried to calculate scrollView height according to elements height in it, but I do not think that it's a right way.
How can I set UIScrollView height dynamically? What is the correct way for doing it?
Your ScrollView should add a ContentView like this:
select your scrollView and in size Inspector bottom, set the width like this:
for more details on how to handle scrollView in Interface Builder your can check out this AppleDoc Working with Scroll Views
Hope this help you!
You only way to set height of your scrollView is by calculating the height of individual objects added to scrollview.
Programatically, calculate the height of each UI object and at the end set content height to scrollview.
[scView setContentSize:CGSizeMake(300, height)];
In above line, 'height' is nothing but a sum of all heights of all UI objects calculated.
By doing the below stuff's, I was able to implement UIScrollView with Automatic height based on the contents inside.
Add scrollview to the main view.
Add one ContentView inside this container scroll view.
Give proper constraints inside this content
view to the container scroll view as well as to its internal UI
Components. Please note that you should not give fixed height to the
content View. The constraints of the content view should grow
according to the UI Components inside
This might give you a layout
warning as 'Unambigous.... '. To fix this, give 'Equal Heights' constraint relation between content view and scroll view
Try the above steps, and this will solve your issue :)
Do you want to add item (UIView, UILabel...) in your UIScrollView by auto layout? If so, just put these in it and create correct constraints for them.
The key point is each item which in UIScrollView have top, leading, bottom and trailing side constraints to their superview. Most important is Make Sure that your UIScrollView has top, leading, bottom and trailing Descendant Constraints to its sub-items. It will calculate correct content height of scroll view for you.
In your solution, if your contentView has zero frame, it will not give you any height for scroll view.
I wanted to add some labels and textfields on my view and that view should scroll, so I am thinking of putting view on scrollview.
I wanted to add some labels and textfields on my view
And that view should scroll
So I am thinking of putting scrollView
And then I wanted to put my view having labels and Textfields
Is it possible?
Please check scrollview property Bounces Vertically is checked or not if not check it
May be, your content is less, than ScrollView, that's why scrolling is disabled
Put view with labels in scrollview. Scrollview should be less in height than view. View should have height considering all ui cvontrols in it. should work like this
Make sure your scroll view height is not greater than view its content size has to be greater as all people says. You get confused between content size and scroll view height.
Meas using nib suppose your scroll view height is 300 and when you use the code content size should be greater than 480 so it will be scroll-able on iPhone 4 size device who's height is 480
To make sure your constraints are well defined, first check these steps:
Make sure your base view is a UIView
Put into a UIScrollView and add constraints to the edges of UIView
Put into the scrollview a UIView and add constraints to the edges of the scrollview. I call this "content view".
You should assign a width to the content view. I usually set the width of the content view equal to the width of the scrollview.
Put your labels/views/images/whatever into the content view and use autolayout constraints to resize them automatically to fit the target screen.
Please consider that:
Your scrollview must always have a height, fixed or dynamic. In order to avoid errors with autolayout, consider that:
the last element on the bottom of the content view must always have a constraint to the bottom edge of the scrollview, or
the content view must have a fixed height
If the height of your content view is less than the height of the scrollview, the view will not scroll. You should add more views or more margin to the bottom constraint of the content view.
I'm trying to allow the scroll view to adjust size depending on the size of a subview on this page. All of the other constraints for the objects in the picture work fine.
When I add constraints to my subview to hug to the left, right, and bottom of the screen (and the top of the subview hugs to the bottom of the segmented control) and then update frames, it makes the height of the subview 0.
I can't add a height constraint to the subview because the size of the subview will change dynamically depending on the amount of content in it, which will then change the size of the scroll view.
How can I accomplish this without adding a height constraint?
Your view hierarchy should look like :
Create a single child view of the UIScrollView where we will put all
of our content
The content view has to be an explicit size (or a placeholder size in
interface builder and set at run time). In other words your content
view cannot depend on the scroll view to get its size. It can,
however, depend on views outside of the scroll view to get its size.
From Apple Technical Note TN2154
Set translatesAutoresizingMaskIntoConstraints to NO on all views
involved.
Position and size your scroll view with constraints external
to the scroll view.
Use constraints to lay out the subviews within the
scroll view, being sure that the constraints tie to all four edges of
the scroll view and do not rely on the scroll view to get their size.
UIScrollView And Autolayout
Using UIScrollView with Auto Layout in iOS
Try giving the Equl Heights & Equal Width constraints to your Subview.
The problem I am forcing is:
I have 1 big scrollView and I have to add to it 5 subviews programmatically
when I don't know what heights the sub views will have :(
Is there some API to do so ?
or I have to count the subviews heights manually and add them with CGRectMake
based by calculated sizes?
What I would do is use Auto Layout. If you add your subviews to the scroll view using internal constraints, then these constraints can determine not only the distance of the subviews from one another but also the distance from the imaginary content view that surrounds them. The scroll view then uses the size of the content view as its contentSize, and the whole thing becomes scrollable. And this works despite the fact that you don't know the heights of any of the views, which is exactly what you're after.