Xcode: align objects with the View Controller margins - swift

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.

Related

How to apply full height to a splash screen in iPhoneX - Storyboard

I'm trying to apply a full height to the following splash screen
But it doesn't work even by setting all the imagens constraints to 0
Maybe is something related with the safe area
Here is the graphical representation what you need to do. You need to add constraint from superview not from safe area
You need to ignore the safe area insets. Besides the constraint value that you're setting in the image, there is down arrow from the menu you getting by pressing the down arrow select super view and you'll get the result you need.
Note: Do this for both top and bottom to make it full screen. Also, don't forget to remove the existing constraints if you've already created them.

NSOutlineView disclosure triangle vertically off center

I'm working with an NSOutlineView on a macOS app and it provides disclosure triangles for items that can be expanded.
I'm also creating custom NSTableCellView items rather than using basic cell items. This allows me to create the cell how I want visually.
My issue is that when they're displayed, the disclosure triangle on the left is not centered vertically.
Notice how the disclosure triangles are not aligned properly. They're a bit lower than they should be. If you scroll away and come back, sometimes, they automatically align themselves correctly. Has anyone been able to fix this issue before?
For what it's worth, I'm using the following code as well for the cells.
self.outlineView.rowHeight = CGFloat(integerLiteral: 66)
self.outlineView.usesAutomaticRowHeights = true
It's hard to figure out what the problem is without seeing how you've set up your project, but I'm going to give it shot.
First, when usesAutomaticRowHeights is set to true, the outline view uses Auto Layout to position the cell views. Thus, you need to be utilizing constraints in your Storyboard or Nib file, or things will behave strangely (see: your picture). If I had to guess, the prototype cell view you set up in Interface Builder is having its autoresizingMask translated into Auto Layout constraints (which, generally, causes a boatload of problems).
What I would do is this:
Open up the Storyboard or Nib document containing the outline view.
Locate the prototype NSTableCellView instance that contains the street name text field in the Document Outline to the left of the canvas. (If you don't see the Document Outline, you can open it by clicking the item at Editor » Show Document Outline in the main menu).
Next, see if you have any constraints in place. If you do, remove them by selecting Editor » Resolve Auto Layout Issues » Clear Constraints under the menu item "section" that's titled All Views in ${YOUR_SCENE}.
Now, depending on what you're going for, there are different ways to go about setting up constraints, but here's what I would suggest. Assuming you want the street name to be centered vertically with the disclosure triangle, I would add a vertical constraint between the text field and its parent cell view like so:
With the text field selected in the Document Outline, click the Align icon in the lower right-hand corner of Interface Builder's main canvas area (see image).
In the popover that appears, check the checkbox next to Vertically in Container.
In the text field on the right side of the popover, enter a value of “0”.
Finally, click the “Add 1 Constraint” button.
You’ll probably see a red error (or yellow warning) sign show up, as the view has now opted into Auto Layout, but it only has a metric for its vertical position. So we now need to add some constraints to describe where the text field should be positioned on the x axis. Like before, we’ll define the constraints using the popover buttons on the lower right-hand side of the canvas:
Click the Add New Constraints button (the one to the right of the Align button).
On the diagram at the top of the popover, click the faint red lines on the left and right side of the white rectangle. This is telling Interface Builder we want to add leading and trailing constraints.
Now, enter the desired padding you want on each side of the text field. In the example image, I went with “4” points on both sides, but obviously, you can use whatever value(s) you think works best with your layout.
Finally, click the “Add 2 Constraints” button.
Any warning(s) that were present should now disappear, as we've added enough constraints to describe the position of the text field. In theory, you should now be able to build and run your project, and the text fields should be aligned with the disclosure triangles. With that said, there are plenty of other reasons a layout can get finicky, and considering usesAutomaticRowHeights is a new API in macOS High Sierra (and Steve Jobs is no longer there to beat it into Apple developers to make everything Just Work™), there could be issues that I'm unaware of.
Alternatively, you can set usesAutomaticRowHeights to false and have some object (e.g. a view controller, a NSObject subclass, etc.) conform to the NSOutlineViewDelegate protocol and implement the outlineView(_:heightOfRowByItem:) method to return any arbitrary height you want for different rows. The nice thing about sizing rows this way is that you can allow certain rows to be larger or smaller, depending on the role of the corresponding item. There are lots of tutorials on this, so I won't regurgitate a half-baked explanation here, but feel free to Google “Conforming to NSOutlineViewDelegate protocol” for more info.
Anyway, try the steps above, and see if they do anything for you, and if they don't, let me know. I can go as deep into the rabbit hole with you as you need, so just ask. Good luck!
For those like me who stumbled upon this issue many years later, here's the fix that worked for me, and requires a lot less work.
NSOutlineView has a function frameOfOutlineCell(atRow:) and the documentation states: You can override this method in a subclass to return a custom frame for the outline button cell
You can override the method in order to provide a frame that's actually in the vertical center of the row. And an important point that I learned from trial and error, is that you don't even need to provide that updated frame. Not sure if this is a bug or what, but for me, just calling super.frameOfOutlineCell(atRow: row) in the function override was enough to make the disclosure indicator appear in the correct location.
So my subclass of NSOutlineView is this:
class MyOutlineView: NSOutlineView {
override func frameOfOutlineCell(atRow row: Int) -> NSRect {
super.frameOfOutlineCell(atRow: row)
}
}
That's all. Hopefully that works for others as well!

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.

Autolayout Constraint issue

I've been trying to learn about autolayouts in xcode 5 by watching some youtube videos. On the videos, they say:
switch on autolayout (which I've done)
Then while dragging an object around the storyboard you'll see blue guides (which I do).
Then, when letting go mouse the mouse, a constraint which look like the letter 'I' appears.
However, this doesn't happen for me - it's like constraints aren't being added?
Am I doing something wrong? Hope this isn't too vague!
Shell
After you place a View inside Interface Builder and blue lines did appear,
click on the Resolve auto layout issues button (second to last button in the bottom right corner; looks like circle between vertical lines)
select the Add missing constraints option
The constraints that were "proposed" by by the blue lines should now be automatically added to the view.
Xcode 4 added constraints automatically.
Xcode 5 only adds constraints if you tell it to.
To add constraints in Xcode 5 you need to explicitly lay them out.
You are only one step away...
Turn on auto layout.
Add you element to the view. You will see the blue guidelines.
Add your constraints by holding down CTRL and clicking from the element you added to the one you wish to add a constraint between.
e.g. If you want to add a constraint for the width of the element. CTRL-drag from the elements back to itself.
If you want to add a space constraint to the superview then CTRL-drag from the element to its superview in the direction you want to add the constraint.
If you want to add a space constraint between two elements. CTRL-drag from one element to the other.

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.