Sorry for the super basic question, I'm not understanding why (or how) Autolayout automatically resizes buttons/labels after setting constraints to the safe area/view.
Here is a short video demonstrating my exact problem. I want to keep a 200x100 button centered horizontally near the top of the screen, why does it resize itself after setting constraints?
If anyone could explain/point me to a good guide for getting started with auto layout it would be greatly appreciated!
You need to set constraints for the width and height, not simply set the width and height. Setting constraints will ensure that the width and height do not vary from what you set them as.
Check out a demonstration below of how I set the width of a button equal to 200, the height equal to 100, the offset from the safeArea top to 20 and horizontally centering the button:
You should set the constraints for width and height, not just size in Size Inspector. Make sure you set them as follows:
The two basic rules of autolayout are:
Autolayout and frame/size are opposites. You can only use one of them.
If you choose to use autolayout, you must completely determine both position and size using autolayout.
So, at first in your video you are using frame/size. So the button obeys your size settings.
But then you come along and add some constraints. At that moment, you are using autolayout — and your frame/size settings cease to mean anything.
Well, the rule for something like a button or a label is that unless you explicitly set width and height constraints with the desired Constant values, it will size itself to its contents as an automatic fallback (using its own internal constraints that you can’t see). So that is what it does.
The easiest way to add a width or height constraint is to control-drag internally inside the button, horizontally or vertically. But you still might need to set the Constant back to what you wanted it to be (in the constraint's size inspector).
Related
I'm working on a project where I want the following in an NSTableViewCell:
Image
Text
Subtitle text
The NSTableView is in a window which the user can expand or contract. When the window expands and contracts, the text wraps as needed.
That much is working.
When it comes to the image view, I can't get the thing to resize at all. I don't understand how the text automatically wraps, but images don't automatically scale. I've been working on iOS so long that I might have missed something in how stack views differ between iOS and macOS, but I never had this problem in iOS.
I don't have much code because the text wraps properly without any code at all, so instead I posted a minimal project showing the problem on Github:
NSImageTableViewTest
Some things I tried:
I have to set the width/height of the image view, or the text won't wrap. It seems to me that the reason why is that if I keep the image view unbound, the table view starts at the width of the image.
I tried setting the leading and bottom constraints of the image view to no avail.
I tried setting the constraints of the NSStackView, but that doesn't help constrain the frame of the image view.
Question: do I have to change the frame of the image view in code? I did try that, to no avail.
At this point, I'm stumped and I'm sure the fix is something easy that I overlooked.
Thanks.
The image view doesn't shrink because the default Content Compression Resistance Priority is too high. Set the Content Compression Resistance Priority to (a bit lower than) "Low (250)".
I am creating a simple app for storing some permanent values and am stuck trying to get the layout how I want. I have done some basic autolayout tutorials, but I can't seem to condense this question into Google searchable format.
I am trying to set constraints for a Container View, which I want to be
centered horizontally, and
a set number of pixels from the item above it (actually, I'd love it to be a % of the screen down from the next element up, but I'm new to layouts).
For the other items in the View, I am able to set just these 2 constraints, and everything is blue.
Layout of Label
When I apply the same constraints to this Container View, the constraints are Red, and the Container View does not appear correctly when I run the app.
Layout of Container View
Is it the case that I need to learn a lot more about Container Views or Autolayout, or is it something simple and different about this particular view/element?
Thank you for any feedback.
UILabels' intristic size vary from the length of the text they contain.
Since a container view does not have such a thing, you should set up leading/trailing constraints or a width and a center constraint for it.
I would recommend to also do this for the labels, since they can easily be too long and be partially hidden this way.
A designer I'm working with wants to have a UIImageView be at a specific y position, with a specific width/height depending on the iphone size. I've done research and I haven't been able to find anything online that allows me to do so with constraints on the story board. I want to avoid having to use code because throughout the rest of the app there are a lot of similar situations. In the past I've made different storyboards for each phone size to accommodate for the specificity our clients request, but I want to avoid doing this because it feels like bad practice. Is this even possible?
This can be completely handled in the Interface Builder. Follow the steps below.
1) Add your UIImageView to the ViewController
2) Set constraints for Width, Height, Vertically Align and Horizontally Align
3) You can adjust the Y position in the Size Inspector of the view.
4) Now switch to Assistant Editor and choose 'Preview' and add different screen sizes to see the auto layout in action.
All of this will set a static image size of 250x200, horizontally centered, and vertically at 1/4 of the screen height.
Little-known capability of constraints are that you can set view positions, or in your case sizes, by way of ratios against other views.
With just your image view selected, the tie-fighter :) constraints button lets you specify absolute positions & sizes: do that to set your desired y-position. But then select both your image view and its enclosing parent view, then the pop up for that same button now enables the equal height/width buttons. Add those constraints and then edit the constraint to change the 1:1 ratio to whatever ratio you need.
Excuse my lack of screenshots, posting from my phone.
I was just experimenting with UIStackView. In this quiz app i have different sized imaged, so i want the image to fill the space between the top button and the question text at the top. The buttons have constraints on them so they should be any size under 50px. As of now the top button is the one getting auto increased size, but that seems kinda random.
According to what I understand you have all the items inside a vertical UIstack. The images have different sizes so the available space for the buttons will changes based on the size of the image. I suggest that you add constraints to all buttons to be equal in height. This way they will re-size based on the available space but they will all have the same height.
I have read this post and I have tried with many examples but i could not put two images one beside the other, each one with a width 50% and at the same time that this images maintain their aspect ratio. I do not understand what restrictions need to.
I have this:
In the image you can see the viewcontroller structure, 2 imageview and the constraints.
Please I need help, at least one similar example. Thanks.
I can see you have missing constraints on your scene, so that could be an issue.
I would put the UIImageViews in a container view pinned to your main view, making sure there are no missing constraints. The benefit of this is in debugging - you can give it a different BG colour and make sure it's resizing itself properly on the simulator/device. I found that putting the image views inside this helps enormously.
Then you need to specify Aspect Ratio constraints on your image views, and also make them equal widths and heights. Add some small constant horizontal spacing between your image views and top/leading/trailing to the container view, and let auto-layout decide the optimal image sizes.
I've included a screenshot of my storyboard:-
This renders like so:-