make four buttons width to fit for all screen size in swift - swift

I am doing a sample project with size class and autolayout in swift.I have four buttons on top of the screen.How to set same width for all buttons with one pixel gap between them and make them fit for all screen size.
thanks in advance

The trick here is to make the button widths variable, but the same for all buttons. This can be accomplished like this:
Place the four buttons
Set the left margin constraint from the leftmost button
Set the right margin constraint from the rightmost button
Set horziontal distance constraints between the buttons to 1
Set the width constraints for all buttons to >= 10 (or any other
value, this makes the width variable)
Set equal width constraints on all buttons (ctrl-drag from one button to the next and choose equal width. Repeat that for all buttons)

Related

Horizontal Stackview in swift: how to wrap according content

I use a stackview (horizontal) with this setup:
Inside it, I have just 3 buttons (without text, only icon). The height/width button are set by constraints:
And the result is:
BUT I don't understand why my second button has a huge width... even if it width is fixed at 44.
Have you got some explanations guys please?
Thank you very much!
Your settings are just conflicting, that's the answer. It must fill stackview width with 3 buttons 44pt each + 6pt spacing between them, and this can't be resolved, because stackview width is bigger than 44*3 + 6*(3-1).
If you want all buttons be on the left with 6 point spacing, then remove trailing constraint for stackview;
if you want buttons to fill the full width -- then remove spacing = 6 and set distribution = equal spacing
Try to put the distribution to equal spacing on the stack view. That means that the spacing will be equal between every button. If you do not have any constrains on the stack view that affects the width then that should work.
Otherwise you need to increment the spacing value in order for the buttons to have equal widths.
Thanks for the question!

How to get exactly the same point on different screen sizes?

I want to call the action (go to another view) when user tap specific area of image (black dots): . Image fills whole view, content mode is 'Aspect Fit'. The problem is that when I setup it on one screen size (e.g. iPhone 8) on another the 'tap area' is shifted. I've tried to solve this with button and constraints or UITapGestureRecognizer with point conversion using screen resolution (nativeBounds), but nothing helps.
It is possible to use constraints to match the positions of the circles with UIButtons. The trick is to use the multiplier of the constraint to scale the buttons width/height and position to the screen size.
I'll describe how to do it for one button, and then you can repeat it for the others. I assume the image is 657 wide by 918 high. If I have the dimensions reversed, you'll need to substitute the actual values for the ones I have used.
Create a UIView to hold the image and buttons. Give this view an aspect ratio constraint with multiplier 657:918 which is width:height. Add the UIImageView to this view and constrain its 4 edges to the edges of the view with 0 offsets. Give this view constraints to the left and right edges of the main view and give it a vertical constraint to place it on the screen.
Get the width/height of the circle in the image and the horizontal and vertical positions of the right edge and bottom edge. For example, the topmost circle is 106 x 106 and ends at horizontal position 392 and the bottom is at 338.
Set the width of the button equal to the width of the containing view with multiplier 106:657 which is width of circle:width of the image.
Set the height of the button equal to the height of the containing view with multiplier 106:918 which is height of circle:height of the image.
Set the trailing edge of the button equal to the trailing edge of the containing view with multiplier 392:657 which is end of circle:width of image.
Set the bottom edge of the button equal to the bottom edge of the containing view with multiplier 338:918 which is bottom of circle:height of the image.
This will allow the button to stay aligned with the circle on all devices. Repeat steps 2 through 6 for the other circles.
Instead of using an image, you can try creating your own UIView subclass called BlackDotsView.
In the draw(_rect:) method, you can draw the lines. To determine where the lines start and end, you need to do some maths with the view's width and height. You calculate where all the lines end and create UIBezierPaths and then you stroke the paths.
In the initializer of BlackDotsView, you can add the dots as subviews. To make them circular, just set dotView.layer.cornerRadius to half the dot's width. Then, you can add UITapGestureRecognizers to the dot views.
You can follow the delegate pattern by creating a BlackDotsViewDelegate that has a method called dotTapped(index:). When a dot is tapped, you would call the delegate method and pass the index of the dot.

Storyboard labels constraints

I have 4 labels on my storyboard, aligned at baseline and with fixed distance between them.
Each label can have different values at runtime, and I would like that the group of these 4 labels is horizontally centered.
Didn't succeed to fix it !
I tried to put them in a view and center the view, but it doesn't work either.
Tried also to play with priorities, but didn't't succeed either...
Is it possible to achieve that in the storyboard ?
You can do this by putting your 4 labels inside of another view. Add the following constraints:
Set fixed distance constraints between the labels (3 constraints).
Set constraints to align the baselines of the labels (3 constraints).
Set a leading constraint from the leftmost label to the containing view. Set the constant to 0.
Set a trailing constraint from the rightmost label to the containing view. Set the constant to 0.
Set a constraint for the height of the view.
Set a constraint from the top of the left label to the top of its containing view.
Set a constraint to center the view horizontally.
Set a constraint to position the view vertically.
The width of the view will be determined by the intrinsic sizes of the labels plus the sizes of the distances in #1. This width will change as the contents of the labels changes, and the view will keep the group centered.
You can use a Stack View for this:
In the 'Attribute Inspector' you can set for example that each text field has an equal width and a spacing of 5 points between them:
You can then use your Stack View to center horizontally like your normally would do.

How to adapt the position of a UIButton to a background image for all iPhone sizes?

I just begin swift and I'm facing a simple problem:
I can't adapt a UIButton to an image in my storyboard for all iPhone sizes. I work with a storyboard with the size of an iPhone 6s Plus because I have one. When I run my test, all it's OK but when I run my test with iPhone 5 simulator, nothing is good.
This image shows the button is in the good place on iPhone 6 plus but no to other sizes.
I tried so many different constraints but nothing seem to work.
In summary: How can I set constraints to position a button at the circle in the background image (as shown in the linked drawing), regardless of phone size?
Try setting your constraints to a percentage.
Control-drag from your button to your view.
Shift-click to select Horizontal Spacing and Vertical Spacing.
Click Add Constraints.
Now, you should have 2 constraints for the button, relative to the superview.
In the Attributes Inspector for each constraint, check to ensure the button's X and Y are relative to the trailing edge and bottom of your superview. For example:
First Item: Button.Center X
Relation: Equal
Second Item: Superview.Trailing
First Item: Button.Center Y
Relation: Equal
Second Item: Superview.Bottom
In each constraint, set the constant value to zero.
Then, set the multiplier value to a percentage or fraction.
For example, if you want the button to be at the halfway point, regardless of device size, enter a multiplier of 50% (or 1/2 or .5 if it won't take the percent value - sometimes it won't accept one version of a value, but it will accept another expression of the same thing).
The percentage is multiplied by the superview's values. The superview's trailing edge is the max X value, and the superview's bottom edge is the max Y value.
Your button appears to be around a third of the way down the view, and about a quarter of the view on the left. So you might try setting the Button.Center X multiplier to 25% or 1/4 or .25, and the Y multiplier to 33% or 1/3 or .33
Note also that under a constraint's Second Item dropdown, you can select whether the value is relative to the margin. If that is not selected, the multiplier uses the full width or height of the superview. Selecting Relative to margin uses the width of the superview minus any trailing or bottom margin. That could throw off your percentages. I recommend leaving Relative to margin unselected when playing with your values.
Here are images for the end constraint values:
Note that the button doesn't seem to be centered until you look at the preview. Running the simulators for the different sized phones gave me the same results as shown in the preview: button correctly centered in the circle.

swift - arrange buttons across many screens

I develop test app with swift ,
I need way to arrange first row in first screen to first row in second screen
to make all button across application look similar
my tries :
match X,Y value , width , height
Do you use Auto Layout ?
You can achieve that by
set vertical space between the top edge and your title
set horizontal alignment of title to be center of the view
set vertical space between title and the first item in the row
set leading margin of the first item in the first row
set horizontal alignment of all following items to be center of the first item