Unable to center/properly scale button and label in view using constraints? - ios

Im having a frustrating problem as I've worked with constraints in the past and have simply used the "Add missing constraints" to do the job, however I am having a LOT of difficulty achieving something very simple -
I have a larger button and a label positioned on my xib file. I need both these centered horizontally and scale with the device, and more or less maintain the distance between each other. Basically just need them to look reasonably good and for the button (which has an image within it) to scale but maintain aspect ratio.
Add missing constraints has not worked in the least as this results in neither the button nor label scaling (both stay small) and the space between them being too big on larger screens. I've tried every other constraint configuration but the button just ends up being squished or going off the screen. Here is what I'm trying to achieve:
The troublemaker is the button. Is there a way to do this programmatically? I am desperate here. How can I configure the constraints?
How do you set a distance between 2 objects?

Center the button horizontally and vertically with the view. Center the label to the view and give vertical spacing from button to the label. Also give width constraint to label if necessary. This will give you the desired effect.

For UIButton:
1.Centre Horizontally & centre vertically
2.Give Aspect ration (or) use leading,trailing,top,button for scale it to different device.
For UILabel
1.Centre it horizontally.
2.Set vertical to the UIButton.
2.Set Aspect ratio.
Hope this helps you.

Related

Constraints to resize buttons to fit any screen - Xcode swift

I don't really understand constraints and have tried many different suggestions found online. All they seem to do is bunch everything up on top of one another or do nothing at all.
I have the following IPad application but I want it to work on any size device, mainly a IPod touch.
The page is simply two buttons that I want to remain the same no matter what screen they're on.
Any help on this appreciated.
It helps to think about points of reference that won't change with different screen sizes. Sometimes you want things on, say the top left corner so you just do constraints to the top and the left.
I'll give you two suggestions
Suggestion One
For your case, it seems like you might want to do constraints off centerY since you want them to be in the middle despite the screen size.
So I would make a constraint to "Center Vertically in Container" and then tap on the constraint and adjust it's value to negative or positive, so that way it's always X pixels above or below the centerY.
Now that's not going to be enough. it knows it's Y position but it doesn't know its height, width, or X position. So you need to add enough constraints to satisfy those.
A few examples:
X/Width: Two constraints to leading and trailing on each button OR Center horizontally and fixed width constraint. (again be careful with fixed width constraints since screen sizes can change, sometimes it's what you want though)
Height: Yeah just give it a height constraint in this case.
Note that this means no matter the screen size they'll always have the same gap between them (and maybe different gaps to the other edges).
Suggestion Two
Use a container view, either a stack view (fill, equal spacing, vertical alignment, a spacing value for gap between) or normal view.
You can make the view a fixed height based off the height and spacing between the buttons you want. Then simply center that container view horizontally and vertically on the super view.
Nonsuggestion
There are certainly other ways (like using buffer views with equal heights constraints. So you'd have an invisible view on top, a view in between and a view on bottom. and you'd give those equal heights constraints and align the buttons to the edges of the invisible views surrounding them. As long as you gave the buttons a fixed height this would work for vertical constraints) but I think these two would probably be the best.

How can I center four buttons horizontally and vertically?

I would like to center four buttons horizontally and vertically. Each of the buttons shall get a background image later, so I think it's important that the aspect ratio stays more or less the same, otherwise the pictures won't fit onto the button.
A screenshot of what it should look like:
All buttons have the same size and should take up as much space as available (small margin, 10px or so, not more if possible). I don't know how to set the constraints that the application looks good on all devices (iPhone 5s and iPad 10.5").
The application must not necessarily run in all orientation modes.
How do I have to set the constraints?
This problem can easily be reduced to the problem of making a maximum square centered in its superview. You can do that easily with just four constraints:
The rest — the grid of buttons — just falls into your lap. For example, make my blue view a UIStackView containing a two UIStackViews, and there's your grid. Or you could do it with explicit constraints yourself.
If this app can rotate, you might need two sets of constraints, one for when the width is less than the height, the other for when the height is less than the width. You would then have to swap them in code. But that's another issue, and is not difficult.
Add them to UIStackView, for placing them horisontally use horisontal value for axis property you can customize it with distribution, alignment and other properties of UIStackView.
It's the easiest way

How to use auto-layout to put things in a percentage of a screen size IOS

So I am trying to make my launch screen for an app. The launch screen is made up of 2 text boxes and 2 UIImageViews.
This is my sketch up of what it will look like
My problem is I can not get auto layout to work with percentages of the screen, instead it has these numbers that I dont quite know what to do with. So while I know that the two image views are a 4th of the screen width from the edges I dont know how to put it their. Also I dont know how to make the image size occupy the same ratio of space on all devices.
Could you please get me started on setting this up?
autolayout does not Have Percentage support but it supports Aspect Ratio.
you need to add one view and add your two imageViews and textFields add equal width and height constraint for ImageViews and same for TextField
you can also add constraints to ImageView to expand in rest of space for that just add margin constraints and equal width and Height of ImageViews.
add aspect Ratio to ImageView's Width and SuperViews width as your requirement.
you can add aspect Ratio with individually to width and Height.
here as you mentioned in que. you can add aspect ration 4:1 for 4th of the screen size.
do same for TextField.
if you still need help than tell me I can explain in depth with screenshot.

Swift Center View Horizontal Ratio

I am creating a view within IB and and attempting to have 3 UILabels evenly space horizontally across the view. I came across this on SO, https://stackoverflow.com/a/30249550/4597666. I have three UILabels, each have the height and width constrained. Here is what the IB looks like:
I constrained each centered horizontally, and the first UILabel I have the multiplier 3:1, second, 3:3, third 3:5 like the post states.
When I run on my emulator, I don't get the result that I was expecting. It appears all three UILabels are centered horizontally, and the first and third are not offset.
Is there another setting that I'm missing, or another way to properly space the views evenly?
you need to make only one change.
Constraint you set is 3.CenterX to Superview.CenterX all you need to do is interchange the value so that you constraint should look like in below image.
Alternative solution. If you want to set constraints currently you have set then change the ratio from "3:5" to "5:3" and similar for all the labels.
Result:
Hope it helps you solving your problem.

Xcode Constraining UILabel vertically over a dynamic UIImageView

I am trying to constrain a label that is sitting on top a UIImageView. The ImageView keeps its aspect ratio for the different screen sizes so its length and width change according to the device.
I am running into an issue if I constrain the top of the label to the top of the image view at (for example) a constant of 58. For the lower resolution image that places the label where I want it visually. However on the higher resolution images that position is not where I want it visually. I have also tried adjusting the top constraint so that the constant is 0 and use the multiplier to adjust the position of the label. This however does not fix the problem and the label ends up at different locations on the image.
I really would not like to have to edit these constraints programmatically as I will have way more labels on different view controllers that would be a pain to program. Really hoping I can achieve this in IB.
I'm confused... why aren't you pinning a constant distance from the top of the label to the bottom of the image view?

Resources