I want to make a UIView's height equal the content that is inside of it. How do I do this? Here's my storyboard
View Controller
Scroll View
View //The view that needs to have dynamic height
UILabel
UIButton
UIView
So, I need to make the View to have dynamic height because the height of the elements inside the view change height on certain circumstances.
Related
I have a custom view that gets loaded from nib that has a number of subviews. Some of these subviews contain labels which can be a dynamic height. This view ends up being added as a subview to a view that is in a scroll view.
Think ScrollView > View > SubView > Dynamic Labels
I am using auto layout constraints and therefore need to set the height of SubView that is required to display all the content in itself, so that the ScrollView will scroll the length necessary to display everything in the SubView.
I know with a UILabel, you can do something like val neededLabelHeight = label.sizeThatFits(CGFloat(width: label.frame.height, height: CGFloat.greatestFiniteMagnitude)) to get the needed height to display on a single label, but when I attempt to do this on a whole custom view loaded from nib, it just has the height of the view I have in my nib file.
Is there something similar for doing it on a view with more subviews, other than calculating the needed hight of the view by summing all the needed heights of its subviews and vertical constraints?
Here is a simple example...
The XIB file looks like this:
Note: the Bottom Label has a constraint of >= 8 to the bottom of the view. That way you don't have to worry about IB showing an error during design. Any extra space at the bottom will automatically "disappear" at run-time.
The Storyboard looks like this:
The scroll view has a red background (to make it easy to see). The labels and views containing buttons are laid-out and constraints are set as we normally set them.
The "Blue" view - TheXIB Container View - has a Height constraint of 120, but is set as a Placeholder that will be removed at run-time, allowing the view loaded from the XIB to control its height. This will be the view to which we add our Labels Holder View from the XIB.
Here's how it looks with a small amount of text:
It's not scrollable, because there isn't enough content.
After tapping the "Add" buttons a few times, it looks like this:
and it is scrollable (we can see it's scrolled down in this image).
For folks who prefer using a "content view" as the "root" view of a scroll view, it works the same (orange view is the content view, inset by 8-pts on each side):
Here is a link to this example project: https://github.com/DonMag/LoadXIBIntoScroll
And here's a gif animation of it in action:
I've added a scrollview to a view in my app, the view is selected from a tab controller and as such is a container and not a UIViewController it is declared as below
class HomeViewController: Container, UIScrollViewDelegate {..
I've added the scrollview in the storyboard and added all of the components to it and I've assigned the delegate from the storyboard and I've placed this code in my ViewController
scrollview.contentSize = CGSize(width:1048, height:scrollview.frame.height)
scrollview.delegate = self
This how my ScrollView looks in my storyboard, you can see that scrollview is my UIScrollView, then I've added a UIView viewScroll and then added four views to viewScroll (View1, View2, View3 and View4), scrollview is sized at 375x340 and viewScroll is set at 1048x340
When I run the app it does not scroll. As it is not a UIViewController, do I need to approach this differently or is there something else I have missed?
I've added the scrollview in the storyboard and set the width as 1050 and height as 330
You should set the width and height of scroll view to be at most as large as its containing view. What you need to set to 1050 and 330 is contentSize of the scroll view - scrollView is only scrollable when its contentSize is larger than its bounds.
If you're setting up your scroll view using Interface Builder, then you'll need to add constraints to its children's edges. For example, in your case you'll have to add leading, trailing, top, and bottom constraints to your View Scroll view. Select it and add following constraints in interface builder:
After that there will be something like this:
When you add edge constraints to UIScrollViews child views, you are hinting the scroll view about its content size. And if scroll view knows its content size and it is larger than scroll view's bounds, you'll have scroll working.
I'm able to resize superiew height depending on subview height by setting up the follwing constraints as shown:
]
I want to achieve the same effect in UITableViewCell i created from xib. But i it not happening. I have setup the same constraints shown in the pictures for my UITableViewCell. Is there some thing special i need to do to resize UITableViewCell resize itself according to subview height?
Design ui in TableViewCell in xib and not in view. For dynamic cell height according to subview add leading trailing top bottom constraint. dont add height constraint.
In view controller containing tableview instance put code
mytableView.rowHeight = UITableViewAutomaticDimension
mytableView.estimatedRowHeight = 200.0
You have to set rowHeight like this:
tableView.estimatedRowHeight = 250w
I have a View. Inside that view I have Scroll view. Inside that Scroll View I have Content View. I set constraint that content view height and width is equal to View's height and width. The problem is that I have Navigation bar above of my superview. So when I look at simulator on running time it gives me extra space of that navigation bar inside UIScrollview. So have can I adjust height of content view.
I want to create constratin like this . Content view height = superview height - 66. 66 is the height of navigation bar...
the better way is inside your main view put Navigation bar on top of the view and put your Scrollview below the Navigation bar and inside that ScrollView Put the Content View
and if you want to put Navigation bar under the Scrollview you should specify the top space constraint of Content View other wise it will give the same output
I added a UIViewController, then in the XIB i dragged a UIScrollView, and a few buttons at the bottom of the page. Then when i build and ran the application, the application scrolls but the button that i added at the extreme bottom of the page can't be seen properly. So i think i will have to set a height for the scrollview. But where in Interfacebuilder i should specify the height ?
If it can't be done in Interfacebuilder, then can someone show me how to do it programatically ?
You need to set the contentSize property of the scrollview. You cannot do it in Interface Builder. You need to set it in the code of the view controller that manages the scroll view.
scrollView.contentSize = CGSizeMake(width, height);