Align 4 labels horizontally with auto layout in swift - swift

i want to align 4 labels horizontally.
This is what i do with constraints in my storyboard :
It's correct for iphone 5 :
It's not correct for others Iphone like Iphone 6 :
How can i do ? I tried to embed my 4 labels in a view but it's not working too.
Thanks !

The last box doesn't have a width constraint, as the other 3 have. Just add it and it will work.

Put in a "positioning" label in between labels 2 and 3. This label is just used to set the center point in auto layouts. Once you've centered this label, you can then set the other labels to be a fixed distance from this center layout.
The result will be the gutter to the left of the 1st label and to the right of the 4th label will expand/shrink based on the device size.
Be sure to put no content or styling on this positioning label, it won't be visible.

Related

NSTableView Horizontal Scroll Cuts off last column

I created a brand new Swift Cocoa Macos project, and placed a NSTableView. I auto layouted it, and then ran the app, and got this result: https://i.stack.imgur.com/bQmQD.png
If I shrink the window down just a tiny bit more, then the horizontal scroll bar will appear.
Steps to reproduce using XCode 12:
Create a new Macos App using storyboards
Add a NSTableView, and in interface builder place it on the top left corner. Proceed to make it the same size of the window.
Add 4 constraints on all sides of the NSTableView so that it is the same size as your window, even if you resize the window.
Add 3 more columns and name your columns whatever you want.
Proceed to run the app, shrink the right border of the window, and you can see, you can shrink your window past the point of last column, so it's possible that the last column is obstructed and the H-Scrollbar is still not visible. It only shows when you shrink the window even more.
Desired Behavior:
As soon as I resize the window so it can now no longer fit all the columns, the horizontal scroll bar appears.
Is there anyway to solve this, like a hidden setting?
I've tried reproducing this 4 times, and I was successful.
The fix is to set Column Sizing of your table view to your desired settings.

How to auto Layout Labels

I want to layout my labels like this using auto layout. It works fine in iPad 10.5. But when I move to any other versions of iPad with a larger screen, this design brokes. Anyone, please help me to solve this problem.
Swift 4
Use stack view
Step 1. insert labels inside of stack view
like that
Step 2. add constraints on stack view
from top, bottom, left and right alignment according to your requirement
like that ->
Step 3.
Use Stackview properties
a.) set axis of Stack view Horizontally as well as vertically.
b.) set alignment according to requirements.
c.) set Distribution according to requirements (here i used fill equally).
b.) set Spacing between labels .
See here ->
Now run. it will work, it will show same in all Device like iPhone and iPad and also (horizontal and vertical).
it will show like this(iPhone6splus)
Thank you
It seems like you create/checked constrations only for iPad or iPhone. Change a type of device scoping on Storyboard editor and you can find errors. Take care about UI for all of device types.
You can add constraints by "Add missing Constraints".
1.select your labels
2.And click the "Add missing Constraints"

Xcode: align objects with the View Controller margins

I'm starting to study Swift with the help of this Apple's guide : https://itunes.apple.com/it/book/app-development-with-swift/id1219117996?mt=11
Now i am stuck on a small problem that i would like to understand how to solve, but from the guide it is not very clear.
I'm at the 398 page : “Guided Project: Personality Quiz”.
Initially i have to place 4 labels in all the corners of the view controller.
From the guide:
“To hold your emoji in their respective corners on all screen sizes,
you'll need to add two constraints to each label. Begin by selecting
the top-left label and clicking the Add New Constraints button. Enable
the top and leading constraints and set them both to 0 pixels,
ensuring there's no space between the edges of the label and the
margins of the view. By default, the top of a view has a 20-pixel
margin, and the left and right sides have 16 pixels of margin. So when
you enter 0 pixels, you're actually telling the label to position
itself 20 pixels from the top and 16 pixels from the left edge of the
view. Add these two constraints”
So, following the guide, i place the first label in the upper left corner, and add the two constraints
And this is the result. The label is aligned to the Safe Area and not to the view margins.
Looking to another project (not mine), i think the two constraints that i need are these (or something similar):
I'd describe that as a bug in Xcode. You asked for margin-relative constraints and you didn't get them. That's not very nice!
You'll just have to edit the constraints afterward. In your final screen shot, double click a constraint. Use the pop-up menu to change the Safe Area constraint to a Superview constraint. Then choose from the menu again to check Relative to Margin. Then you might have to fix the Constant value. Do that for the other constraint too.
Either that or just don't use the constraint popover to begin with. Instead, draw your constraint by control-dragging from the button and use the HUD that appears. Hold Option in the HUD to get superview margin constraints.
EDIT Ooooh, here's another workaround; before you form your constraints, hide the safe area layout guides (uncheck the 4th checkbox in this screen shot):
Now the constraint popover works correctly.

Swift - Auto constraints

I am trying to use the auto size classes from the storyboard on xcode 7 to position UI elements in a controller. The problem that I'm running into is that when I try to use the "Add missing constraints" function (located at the bottom right corner of the console), it positions my UI elements correctly except for the last elements (pictures describe better). The first image below shows the storyboard file where I just want 3 buttons (stacked above eachother) to be the same width and length to be on the top right corner of any screen.
However, when I add constraints and run the simulations, it seems like the top two buttons are positioned correctly with the correct length and width but the third button is out of place (image below).
So my question is, am I forgetting a step to make all buttons position themselves? Or should I try to convert everything to a percentage and place UI elements based on the percentage of the screen (if so, how would I go about doing that)?
I've also tried adding another blank button (removing the button label) underneath the 3rd button and adding constraints like that but it didn't work for me. Let me know if you have any suggestions, thanks!
In you case, Autolayout the constraints you need to give to UIButton is 4 constrains.
Leading
Trailing
Width
Height
If you miss any of them, then surely you will get an error. So, what's your error is?
To the third UIButton, you have not given the height, while to the above two buttons you have given.
So, just remove the bottom constraint of UIButton and give the equal height to above UIButton.
FYI, never use Add Missing Constraints without any confirmation from your side.
Update:
Check this video to remove trailing or leading margin:
http://sendvid.com/1h8deg18
You can actually see the solution in action if you use the Preview screen while setting up your auto layout constraints. I just created a similar view and buttons and stepped through the process. I coloured the buttons and named them to make things obvious.
I added the three buttons. At this point, none of the buttons show up in the preview.
I then setup the auto layout constraints for Button1. If you want the buttons anchored to the top right, then you don't need to worry about the leading constraint. You need width, height, top, and trailing.
Now Button1 will snap to it's position in the top right corner of the preview screen.
Now do the same thing for Button2. Set width, height, top (vertical space to Button1), and trailing.
Button2 will now snap into place in the preview.
Now the same thing again for Button3. You anchored the first button to the top right of the screen. Then Button2 to the bottom of Button1 and the right edge of the screen. Then again for Button3. You could also align the edges of the 2nd and 3rd buttons, if you prefer that to trailing space.
Now you'll see in the preview that your buttons are correctly positioned, regardless of device.
As long as you specify height and width for each button, you don't need to worry about the left edge or the bottom edge of the screen at all. They each know to "stick" to the top right and they know what size to be.
** Note: If you're not familiar with the "Preview" option...
With your storyboard open, hold Option and select storyboard again to get another copy of the storyboard on the right side. Highlight the view controller you are interested in on the left side. On the right side, select the Preview option as shown below.
Now you have your storyboard and the preview side by side, so you can see the exact impact of any auto layout changes you make. You can also add or remove devices to the preview.

Remove iOS 6 ScrollView Center Alignment

I am trying to create a page control in iOS 6.
I am using the interface builder to create the scrollView and have stretched out the screen using freeform simulated metrics.
There are 5 different views in the scrollview and the problem arises with the center view. I would like to align this to the left view and not to the center of the superview.The problem is when I set it to be 0pt from the left view it automatically creates a constraint Align Center X to Superview.
Align Center X to Superview is causing an unable to satisfy constraints crash.
How can I fix/remove this?
Select page 2 and page 3, and choose Editor > Pin > Horizontal Spacing. This creates a constraint between the right edge of page 2 and the left edge of page 3. Now you can select and delete the centering constraint.
Basically the rule is simply that you can't remove a constraint in Xcode unless other constraints exist that completely determine the position / size of everything. You create those constraints with the Pin menu.