I have a ViewController which contains a ScrollView and I'm trying to make a form menu so I need multiple labels and text fields.
I've been using the designer to do that.
The ScrollView will allow the user to scroll vertically, I already have many elements in the View but I need to add more.
The problem is that the size of the parent ViewController have a fixed size in the designer and because of that I reached the situation where I need to add elements under a label but there is just no space in the designer where I can put it.
hmmm.. not sure how this will carry over to tamarin/visual-studio, but in Xcode Interface Builder you can set the View Controller Simulated Size to "Freeform" --- and then set it as tall as you wish. Well, it may have a limit, but I just tested it and made it as tall as 5,000 pts. After laying everything out, you can set it back to "normal" size. At runtime, it will size itself however you've set the constraints.
Other alternatives...
Design your elements in "containing" views, and then add them in either dynamically via code or by manually setting the positions in the designer.
Use a single, tall "containing" view in your Scroll View. Make that view, say, 1500 its tall. In the designer, add the first few elements, then set the Y position of that view to a negative number. If your Scroll View is 500 its tall, set the Y position of the containing view to -400. The view will "slide up" in the Scroll View, and you can continue adding elements.
Related
This is the same as asking how to achieve wrap_content in iOS.
However, I found no suitable solution. Almost all solutions suggest to override intrinsicContentSize(). What if the view does not know any information about its child's height?
The case is like this:
I want to create a BaseDialogViewController that is responsible for blurring the background and displaying a dialog container. Then if I want to implement a dialog, I will extends BaseDialogViewController and add the dialog content to the container view.
So, the container needs to adjust its height according to the total height of the content inside of it.
How do I achieve this?
Just set constraints for all borders from the child view to the container view. You should ensure that the child view can compute its size from its contained elements, by setting constrains for all borders to its child elements, and so on.
The container view should only have additional constraints for its position (e. g. centering in its parent view).
The rest is done by Autolayout. It will calculate the size from the innermost elements with fixed or intrinsic sizes (e.g. labels, text fields, buttons) to the outer elements.
I realized that if child views height is specified, or has an intrinsic height,
you can set constraint from parent's bottom to child's bottom, and by setting this, the parent's height will be able to vary according to the bottom position of child's view.
I found this out from this answer.
I have an app using scroll view on a page with numerous icons within that scroll view. I don't understand auto layout very well, but I have constrained every single icon to its current canvas value to the view (after measuring and placing each icon in the correct spot). Why are there still conflicts (188 to be precise) and unambiguous positions?
As an example of what I have done in case the above is not clear, I have placed an icon in the top left of the scroll view. I have then constrained that icon on top, bottom, left and right of its current canvas value from the view.
This extremely hard to explain without showing (as in "live-demonstrating"), but I'll give it a try:
Autolayout for Scrollviews works differently than you probably think. You can't simply specify where to place its contents in relation to its bounds like that. After all, the "canvas" of the scroll view determines its size from the content you scroll over, whereas the size of the actual scroll view (lets call it its viewport, or "window onto the canvas") is determined in a different way (for example by constraining it to the elements around it).
This the opposite of how autolayout works for other views, since they are usually constrained by the size of their parent (up to the top view of a view controller and/or the screen bounds).
What you want to do is add all your items in a single view. From the smallest known size, constrain them to one another. Finally their container has a size resulting from its contents, i.e. your items. This is then the size of the "canvas" of the scroll view, so add it and constrain its position somehow (for example centering it horizontally and vertically). Now, the actual size of the scroll view and its position should be constrained by elements surrounding it.
It might take some thinking and training, so maybe you will want to test around in a small training project that only has a limited number of views to figure it out.
Add scroll view on the screen with constraints to parent view
Add content inside of scroll view
Put constraints between added view and parent view (NOT SCROLL VIEW)
I created a scroll view in Xcode that works awesome because of this video.
https://www.youtube.com/watch?v=3PIm8-lKAYw
When I was messing around after I made it I found out that if I clicked on the scroll view and went to Show the Size Inspector or the fifth button on the right hand side of the screen I had the option to make a constraint called Top Space change in value that caused the scroll view to become bigger and smaller. I decided to see if I could find a way to change the constraint programmatically by simply using dot notation and the equaling it to an int value that I wanted.
So what I'm trying to figure out is there a simple way to change these constraints values programmatically that change the scroll view constraints values?
Without actually following that tutorial (and there being no code in your question) I'm going to make a few assumptions.
Yes, it is possible to change the scroll view's content size by manipulating constraints in your code. If you are creating the constraints in a xib or storyboard, you will need to make sure they are hooked up to IBOutlets so that you can access them in your code.
If you have 2 views arranged vertically that affect the vertical content size of your scroll view, increasing the space between these views would also increase the vertical size of your scrollview's content size. The following would increase the space between 2 views by 20 (assuming a multiplier of 1) and subsequently increase the scrollview's vertical space by the same amount.
// This is a vertical space constraint created in your xib or storyboard between 2 views that drive the content size of your scrollview
someVerticalSpaceConstraint.constant += 20;
TL;DR: What's the best way to layout over-sized scroll-view content in Interface Builder?
I am trying to figure out the least painful way to layout offscreen / scrolling content in Interface Builder. My current approach has been to tweak scrollview content width and height constraints to see all content in IB, before reverting to proper contraints for building, but this gets to be a hassle. That said, I would prefer to do more more graphically, and less programmatically for general ease of editing.
The general approaches that I can think of are:
use embedded segues to build up views in a size-accurate fashion (the most intuitive, flow-charty approach)
tweak constraints for editing, reset for building
use placeholder values / constraints in IB (haven't tried this yet)
layout over-sized content programmatically
In more detail, I am trying to build a vertically scrolling view comprised of sub views. Each sub view is self contained and can potentially appear in more than one context and I would like to keep them isolated (in fact, I am embedding them as well, but that does not affect the question). What I would like to do, is use IB to layout a tall composite content view. I would then like to create the outer scroll view with a single content view again of a container view, embedding the composite content view. Ie. the grey content view on the left embeds the taller red / blue content on the right, and I am wondering if I can deduce the height, so it could be device agnostic (the views are square, so they will take their width from the device)
So summarized, is there anyway that I could derive the hosting scrollview's contentView height from the actual height of the content of the embedded view, or should I instead use approaches 2, 3, or other?
To be clear, Auto Layout requires that scroll view content views define constraints for the edges, and width / height dimensions. In my case, I would like to be able to preset the width to the device width, but leave the height to be derived from the red/blue content view. IB won't let me drag constraints between the embedding and embedded scenes in Storyboard, and when I leave the field missing, it uses the prototype values for the content height.
Is it recommended to use placeholder sizes (3) in IB to make the content visible for editing, knowing that run-time constraints will render it properly (ie over-size scroll views in Storyboard so all scrolling content is visible)? Conversely, is it foolish to even try - should I do all scroll-view content composition programmatically (4)?
Thanks in advance.
Edit:
Placeholder constraints allow you to get by, but force you to change the priority of the run-time constraints, and trigger IB / layout errors and warnings for conflicting constraints, despite selecting the placeholder checkbox on conflicting design-time-only constraints.
Use a UITableviewController with static cells. The cells will be your content views.
You can scroll the tableview down in IB to see the cells at the bottom and you get a nice extra behaviour like scrolling the views to the correct position when the keyboard is opened so it doesn't hide the textfield you clicked on.
And you don't have to worry about the scroll view at all.
If you want to reuse your content views you can make the table dynamic of course and provide the content views by code.
Here is my problem:
I want to create a view whose height exceeds the height of the iPhone-screen. To deal with that, I am using a UIScrollView which works just fine. I created the scroll view in interface builder and set the content size programmatically to twice the height of the screen (that's enough for my purpose). I can run the code and it works just fine. I can scroll up an down without trouble.
The only thing I don't get is how I can add more items (buttons, labels etc.) to the view in interface builder, since it doesn't allow me to change the size of the view. I was thinking to create a scroll view as a top level object and then place a view (the contentview) of a larger height into it. Now the only thing to do is add all the items to the contentview, which is not possible because in interface builder the contentview has only its regular size.
I am thankful for any thoughts on this :-)
You can change the size of your UIView from the interface builder.
For that you have to go to Interface Builder of particular UIView. From Simulated Metrics *select Size = Freeform* and now you are able to change the size of UIView from interface builder.
The accepted answer is accurate but no longer complete for iOS7 and XCode5. Check out #Whasssaaahhh answer here for more detail.
You can drag items on top of the view in two ways.
1) Change Y-Position: If you select your UIView and in Size Inspector change its Y position to whatever section of the view you want to see (try something like -100 to go down or 100 to go up, play around with whatever numbers work for your screen size) you can drag all your buttons, labels, text fields however you like for each visible section.
2) Drag With Mouse: If you carefully select just the UIView with your mouse and carefully drag it either upwards or downwards and let go, it will snap to the new position so you can edit it. It's easier to "see" this if you drag some objects on it first.
You can change the size of your view and size of your scrollview along with the addition of objects.
add the added object height to the scrollView.contentSize and also add into the height of the view.