Can anybody please provide a simple explanation of NSLayoutAttributeBaseline, NSLayoutAttributeFirstBaseline and NSLayoutAttributeLastBaseline? Why do we need it? What is a good example of using it? Apple's explanation of this topic doesn't seem to be clear and obvious. Can't find good examples throughout the web as well. Thanks in advance!
Baseline applies to views such as UILabel. The baseline would be the position where the bottom of uppercase letters appears. For most other views (if not all others) the baseline and the bottom are the same.
For more you can follow the thread: What is the difference between NSLayoutAttributeBaseline and NSLayoutAttributeBottom?
It is, what is it called. The baseline of the text of a view.
Imagine you have view with text inside, let's say a label. And you have another view with that, but it has a box around it (so it is taller). Aligning the top edge or the bottom edge would have the text on different heights. With this attribute, you can align the text in two views of different kinds.
Here is an example with top, bottom and baseline alignment
Related
Using autolayout in Storyboard, is there a straightforward way to center a group of elements without making them part of a subview?
For example if I have one label 50 and a second element Points, is there a way to hold them together as one and then center it.
Right now, I am able to hold the space between them constant (as it needs to be) and horizontally align but I can't get the two of them together centered. My approach has been to set a leading space before the first element and a trailing space after the second element, but something is throwing it off. I know I could combine them into one label and center it, but I'm looking for a solution that I can reuse every time I have one of these all too common situations.
Thanks for any suggestions.
Image:
Storyboard.
This scenario is very common and very simple to solve: just get rid of the leading and trailing constraints and instead add horizontalCenter to the first label regarding the view. After that all you gotta do is adding horizontalCenter to the second one regarding the first and you're set! Now both of them are linked together and well centered.
If you need more help with this, just share your repo and I'll fork it.
I know your question specified that you don't want to make the two labels part of a subview, but I'm not aware of a good way to do that and thought I would at least mention that the standard / recommended way to handle this situation would be to add "3K" and "Points" to a horizontal stack view, and then horizontally center the stack view and vertically constrain it to "Explorer" above.
This would ultimately require the same or less constraints as what you have now because you wouldn't need to create constraints for the labels inside the stack view.
See Apple references here:
https://developer.apple.com/videos/play/wwdc2015/218/?time=134
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/LayoutUsingStackViews.html
I'm not sure if I understand UIStackView correctly. I have a UIStackView in a UITableViewCell. It contains 3 UILabel and is set to align and distribute Fill.
When my first labels text is not very long, it looks all good. The second and third label take up as much space as they need and they are aligned to the right. The first label takes up the rest of the space.
However, when the first label contains a lot of text, it pushes the other two labels out of the visible area. What do I have to do to make sure the second and third label are always visible and take as much space as they need to display their content. The first label should always take up the rest of the available space and then truncate its text with "..."
This is my configuration in the Storyboard:
And this is how the result looks like in the TableView:
I tried the others solutions mentioned here and found them to be to difficult or unpractical. I didn't want to setup and change layout constraints programmatically so I continued playing around with the values. In the end, I found a very simple solution which does exactly what I wanted.
All I had to do was, setting the "Content Compression Resistance Priority" to be lower on my first label. Second and third label have 750 and also they have 251 and 252 for the "Content Hugging Priority" whereas the first label has a value of 749 and 250. This gives me exactly the results I wanted.
Thank you guys anyways. Sometimes I feel like I find solutions to my problems a lot faster after asking it here. I hope my answer will help somebody else in the future.
This:
Gives me this as a result (notice that I set "isHidden" to true for second and third label on the third row):
you may consider placing those 3 views directly to the contentView instead of using UIStackView, and set the first label trailing to the second's
I am trying to create a simple UI that works on all devices (obviously) and I haven't had much success. The program consists of two labels, a button, an image view and a textfield, I am also using a universal storyboard.
I implemented a top constraint, horizontally centred and fixed the width and height for all labels, buttons and textfields. The only exception I made with the image view was that instead of implementing a top constraint, I used a bottom one instead as I have found that utilising the former would result in it being only partially displayed. I would then switch from the universal storyboard to one with base values. From their I would delete the existing constraints and add new ones after moving the UI elements to their new locations on the different sized screen.
When I run my app on an iPhone 4S, the layout, whilst mostly correct, is still not perfect (i.e. a label is far too close to the image). Does anyone know how I can make my layouts look correct? I have been following this guide, Adaptive Layout Tutorial in iOS 9.
Thanks so much for your help!
[Example of the constraints for the picture1
I am not sure how familiar are you with autolayout, therfore I'd suggest you watch Stanford university lecture regarding autolayout.
In short, according to the lecture and after looking at your picture, you should almost never set constraints with actual numbers. Use "Standart value" and when you can't choose "Standart value" write 0.
I'd recommend watching the above lecture and the rest of the examples in there.
Your label has 2 problematic constraints
1. Top space (30 points) to "how old is your dog"
2. Top space (28 points) to button
That means that your button is 2 points height (really small!!)
Or - because the button has already a fixed height, the label and the text field are too close (and maybe even overlaps the button)
You should delete the top space constrain (to "How old is your dog") and do something else, or give it more points height
Good luck!
I've seen a lot of different topics and suggestions on aligning and inputting buttons/text, but the ways I've seen seem kind of risky.
What is the optimal way, for example, to add two buttons, stack them together, and have them be 10% from the bottom of the screen, and centered horizontally on all devices?
Learn Auto Layout if you haven't yet. Use constraints for achieving the following:
For centrally Horizontal on all devices: Use Center X with SuperView.
For having them 10% from bottom, use multiplier value say 0.10 .
The optimal way would be using storyboard for implementation and use of constraints in a proper way.
For example, as you suggested you want bottom space to be 10% of device height so it can be done with the multiplier in constraints also the horizontal center can be easily done with the same, so you could look for a good tutorial of Auto Layout and constraints for better understanding.
Use your Storyboard and add Auto Layout to your elements. Here is an example:
I have added auto layout
Left
Right
Top
Bottom
If I want to change the distance between the buttons, I just choose the auto layout constraint that I have created between the buttons and changes the constant value to it.
If I say that "Button 4" constant to the top shall be 40, then it´s always 40 no matter what size the phone has. If I change the constant between the buttons to 10 then it´s the same for all sizes.
Your best bet is to use Auto Layout. It takes a bit of learning but once you get used to it you can add constraints pretty quickly and easily in Interface Builder. I can't recommend any particular guide but there are a lot of good ones to be found with a quick Internet search.
Here is an example of constraints that seem to be what you are looking for:
For iOS 9, an even simpler Auto Layout approach would be to use UIStackView.
As you can see, no constraints are needed for the buttons embedded in the stack view, as the stack view lays out the buttons for you. All you have to constrain is the location of the stack view itself.
Here's an example of two vertically stacked buttons, 10% from the bottom of the screen, and centered horizontally for all devices.
In a View Controller I have two UILabels.
I'm trying to start to understand the AutoLayout feature, but I'm finding tutorials using storyboards, complex layouts, scrollViews and other components that are absolutely more complex of what I'm trying to achieve and understand.
This is my very basic setup:
What I simply want - to start to dive in the subject - is:
have the distance between the two labels preserved when testing it on 3.5' and 4' screens
have the bottom and top margin of both the labels to be proportionally preserved when testing it on 3.5' and 4' screens. EDIT: please consider that the desired position it's not the exact center
I have tried to add constraints via the "Pin" option, but with no success at all. Maybe I should put them inside another view to act as a container?
This is a very easy question, but right now this AutoLayout feature is really confusing me. And maybe this can be a good starting point to understand what right now looks to me to be designed by a crazy scientist :)
Put both UILabels in a containerView, and align containerView vertically center.
I would support #RoboticCat for the statement he made. But I would try to deliver what I learned from those videos.
For first requirement:
Ctrl+click on any of the label & drag n drop it on another label. Select "Vertical spacing" option from the popup. this will full fill your first requirement.
For second one:
Ctrl+click on top label & drag n drop it on view of viewController. select "Top space..." option from popup.
repeat previous step between bottom label & view. This time select "Bottom space.. " option from popup.
Now navigate to Size Inspector of each label. There you can find the constraints just created. Click them to perform Edit.
Decrease priority of top & bottom constraints to 500 from concerned labels. This will solve your issue.
Let me know if more info needed.. :)