I have universal app. Subview is rectangle which is positioned inside View.
How can I keep it centered and make it automatically adopt it's width depends on screen width ?
e.g.
In order to center a view within it's superview, you need to check Horizontal Center in Container and Vertical Center In container in the Autolayout Align section:
You could also make use of Size Classes :
With size classes, a storyboard or xib file can be used for any
available screen area. You build your interface as it will look in
most sizes, then update only the parts that need to change when the
available screen size changes.
As you said you want this only in portrait means its not an adaptive layout....So its fairly simple now....
As you want the width of object resize according to screen width... Just pinned right and left edges of screen to adopt width accordingly (or use equal width)
As you not mention about height...so give fixed height to object....
And for making the object to the center of the view controller ....
Use center vertically and horizontally....and you are done..
Here is the image of what I said for better understanding
Related
How do you prevent a UIImageView from resizing after adding a constraint to align horizontally to the center? Every time I run the program with the restraints in place the image that was originally sized 300x300 takes up the entire screen
As soon as an image view comes under control of auto layout, its size is determined by its image — unless you take measures to prevent that. For example, you can add width and height constraints, or you can add external top-and-bottom and leading-and-trailing constraints that set the size relative to the superview or whatever.
(By the way, the very first thing you should always do is examine your interface in the View Debugger. If you really added only a constraint to align horizontally to the center, that is an error — an insufficient constraint — and the View Debugger will let you know of that fact.)
Using constraint, is it possible to vertically center a view in storyboards without creating a container view?
Update: To be clear, I want the describe the vertical centering with constraints, so that it will remain vertically centered on device screens with different sizes. (i.e. I don't want to manually make the top and bottom constraint the same value.)
No, it is not possible without writing code
If you don't want to make any container and also you want to handle from storyboard then you can take a view in middle between them with one pixel and give centre vertical alignment, trailing , leading and height. Attached both UILable and UIImageView with this UIView from top and bottom.
I think your problem might be solve.
I have placed an image on an iOS screen and in the area that remains in the bottom, I would like to place another image in the centre of that space. I could do that with Android using relative layouts. How can this be achieved in iOS auto layout?
Let the image view take up the remaining bottom space (fairly easy to do using auto layout), and then set its contentMode to Center.
This is an simple example.
Just select the Horizontal Center in Container after pin other required constrains.
And this is the final layout constrains:
Hey, I'd like to obtain what you see in the pictures: in Compact Height mode (landscape iphone) both the red and the blue view have to take all screen vertically and half the screen horizontally. In Compact Width mode (portrait iphone)they have to take all the screen horizontally and half the screen vertically. Space between views should be same size in both modes.
I used to think I have to use size classes and auto-layout constraints, but everything I tried failed miserably.
Maybe I have to use a UICollectionView and change flow direction based on orientation (if that is even possible)?
A collection view is probably overkill, because you don't want scrolling and that's the whole point of a collection view--by the time you do the sizing to stop it you'll have done all the work necessary to set a non-scrolling layout.
This is possible with Size Classes in IB. First, In general you will probably find it helpful to name the views in the Document Outline on the left in IB. You will also want to use this outline rather than try to grab the tiny constraint H-lines.
Set up all the constraints except 1) constraints linking the
OrangeView and BlueView to each other, 2) the constraints linking
the OrangeView to the top and left(leading), and 3) The constraints
linking the BlueView to the bottom and right (trailing).
Change the size class setting at the bottom to w-Compact and
h-Any in the funky box system. Now we're designing for a compact width, so views on top of each other.
Create a constraints for vertical space for BlueView.bottom to
OrangeView.Top. Also create constraint for OrangeView to
superview.leading (or ledaing,margin) and BlueView to
superview.trailing.margin. If you select any one of these constraints and look at the Size Inspector on the right (the ruler) you should see an "installed" checkbox not selected, and below that a w-Compact h-Any and another installed box, this one selected.
Now, while keeping the constraint selected just to see what happens, change the sizeClass selector at the bottom to w-Regular h-Any. Notice that in the Document Outline to the left, it should get grayed out.
Now we are designing for regular, so side-to-side. Add constraints linking the views for horizontal space, BlueView.trailing to OrangeView.leading. Also link OrangeView.top to the superview.top or top aligned to BlueView.top, and same for bottoms. You can manually edit the frame first; if not, IB will automatically fill in the wrong values, so edit these after you create them, and verify they are w-Regular and h-Any. With the ViewController selected, select "update frames" and the views should snap to their expected shape for the size class.
Let us know if this works for you or if it was unclear. Good luck!
Suppose that I have the following view controller and this is how I want to see it on all iPhone:
If I run it on iPhone 6 it has the following look:
Here you can notice that UITableView not fit the whole screen and UIImageView doesn't placed at the bottom of the screen.
How can I achieve the required behavior via constraints in XCode 6? I thought that I need the following constraints:
Leading space and top space to container margin for UITableView
Bottom space and trailing space to container margin for UIImageView
Vertical Spacing between UITableView and UIImageView
But it doesn't work as expected even after auto-resolve constraints issues:
Thanks in advance.
Ok, a few things here:
Each view needs enough constraints to define it's x and y position, and it's width and height unambiguously. To start with, go back to Interface builder and delete all of your constraints and lay out the view as you would like it to look. You want to have control over every constraint added, don't let IB automatically resolve the issues, as in all likely hood it won't do what you want.
Do you have an image that is the size you want it to be on screen, once you've factored in #2x, #3x etc? If so, then your job will be easier, as the width and height of the image view can be defined by the width and height of the image (ie the image view's intrinsic content size).
In order to use Autolayout effectively, you need to think about your view holistically, and think about how you want your views to behave when the screen size changes, be clear in your head about the behaviour.
To achieve the layout you want, I would do the following:
Constrain the tableview's leading, top and trailing edges to the superview, with a constant value of 0. This means it can get wider and thinner with the device, it will stretch horizontally, but always stick to the top. This has defined the tableview's x and y position, as well as it's width (height still to go, but keep reading...)
Constrain the image view to match the horizontal centre of it's superview (x position defined) and constrain it's bottom edge to the superviews bottom edge (y position defined). If've you've got the right sized asset, then that will take care of the width and height too. If not, you could go ahead give it explicit width and height constraints.
Now we can constrain the tableview's bottom edge to the top of the image view, with a constant of 0 (ie touching). Note we haven't give the table view an explicit height constraint, so as the device screen grows vertically, the table view will stretch vertically.
Autolayout is hard at first. I'd recommended lots of reading to get over the initial hump, really get to know what a constraint is doing, it's limitations, and the way in which the system parses constraints to translate them into frames. This book is really good, and really helped me learn:
http://www.amazon.co.uk/Auto-Layout-Demystified-Mobile-Programming/dp/0321967194
Best of luck
First make sure you have selected the correct size class. The 'Compact Width | Regular Height' size class must be selected in the Interface Builder. Now add the Trailing space,Leading Space, Top space and Bottom space constraints to the table view. For the image view set the view mode to Aspect fit and add the constraints : Align Center Y ,Top space,Bottom space, Leading space, Trailing space and Aspect Ratio .