Very basic AutoLayout - ios

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.. :)

Related

UIScrollView and variable text size

I'm creating an app with quite a lot of text. It will be of variable size, so I was wondering if there is a way to adapt my ScrollView to the size of the UILabel, not knowing in the IB what will be the number of lines of that label. I'm doing it from the IB, drag n dropping it, then I'm putting 4 elements inside : an image for the logo, a title, another image, and then the label.
My question is : how can I tell my ScrollView to adapt to the text ? With constraints ? In the code ? I'm sooooo lost with that scrolling thing…
I think I need a very very good tutorial about autolayout…
Here's some screenshots of my project
The project on simulator
The layout
Thanks a lot!
You have to add the constraints to your label so that it will always have the same spacing to the scroll view. Also, you will have to set your labels lines to 0.
Don't press Add Missing Constraints because it will probably set your height and you don't want that to happen. Use the Pin button to set the spacing to nearest neighbor.
These are the buttons you will have to use, they are bottom right.
First of all, press the one in the right and select All Views > Clear Constraints.
Then, select your label in the storyboard and press the button with a square Pin, here you will have to select everything until you have something like this with different values... Also click in the red lines so that you get a continuous red line. Don't check any of the boxes you see.
After this, you have to do the same process for your Scroller.
It should be enough. I will also need to know where your scroller is.
If you have anything else in your view, do the same.

Size classes and auto layout for iOS development

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!

Auto height setting for labels in iOS

I have previously worked in Windows phone and see that every control in windows phone has an Auto property, meaning occupy the size of the content.
I see that in iOS such a property does not exist. When there are dynamic data to be bound to a UILabel, I always need to calculate the height of the data and then assign to the UILabel. This takes a good amount of time and bit painful. Is not there an Auto property or am I missing anything here?
iOS has AutoLayout which is really helpful, get familiar with it.
Click on the Label
Click on the pin constraints button (little square button)
Add your custom LEFT, RIGHT, TOP margins or LEFT, RIGHT, BOTTOM margins
Click on "Add 3 Constraints"
Set number of Lines to 0 which means as much lines as view needs
Then you probably got warning lines, but you can solve them
Just click on fix constraints button (little triangle button)
Click update frames
UPDATE
Important: the answer to your question is to PUT NUMBER OF LINES TO 0 you can use that UILabel with 0 lines(which is autosizing) with frames and AutoLayout. AutoLayout is just a friendly suggestion that can be helful to setup views. Also put Line Breaking Word Wrap
Here you go also with some useful links for working with AutoLayout. AutoLayout is great because you don't care anymore what size is the screen, what orientation has the device at that moment. You just need to setup everything correctly and everything works amazingly but if your setup is wrong then AutoLayout might become your enemy. So start learning and experiencing right now.
Very good point to Begin learning AutoLayout
If Your are being lazy, start from video tutorial series
Great iOS7+ table view tutorial with autoresizing cells
Also check out this Stack Overflow discussion
You need to familiarize yourself with Auto Layout:
Auto Layout dynamically calculates the size and position of all the
views in your view hierarchy, based on constraints placed on those
views.
Just give top, left and right constraints and make label's numberOfLines to 0. That's it. Label's height will resize automatically.

How To Add Storyboard Constraints For Equal Spaced Buttons

I am trying to add two buttons to a view that also contains a table view. I want the two buttons to fill the width of the screen for different screen sizes (portrait only). There is a minimum width of say 100 units for the buttons to hold the text, but I would like them to be wider for larger screens. I would like the space between buttons to be at least 20 units and at least 20 units of space between the outside edges. This seems like it should be straight forward, but I can't make sense of the align and pin constraints in the Storyboard auto layout. So far, the tutorials I have seen are confusing and don't seem to cover the right amount of detail to help me. Please provide me with some guidance. Below is the layout before constraints are put in place.
And here is what the layout would look on a 4" iPhone:
I have already spent a day trying different combinations of constraints, but continue to have problems. Just when I think I have something that works, it won't build because it says I have conflicts.
I am trying to use the following tools (along with the align tools - not shown) to create the proper constraints. I have tried selecting one or both buttons and then using the editor to set the constraints, but each attempt looks bad. Even when I choose "Reset to Suggested Constraints", I get wacko results. Please help with suggestions on how to accomplish this seemingly basic task.
You must have:
Leading for first button 20
Trailing for second button 20
horizontal spacing between buttons 20
equal widths for both buttons
width >= 150 for each button

Swift: How can I change the location of Views programmatically?

I want to have certain views in exact places, and constraints aren't working for me. How can I place, say, a collectionView in the lower 80% of the screen, and an ImageView in the top 20%?
In case it matters, I am planning this app to work only for iPads in landscape view.
You can use NSLayoutConstraints, it will not take long to get familiar with the system. This way you can customize your views with ease. Bigger applications are made without storyboards. This a good tutorial http://makeapppie.com/2014/07/26/the-swift-swift-tutorial-how-to-use-uiviews-with-auto-layout-programmatically/
Does it have to be programmatically? You could add constraint between the two in the interface builder and achieve the same. To do this,
control drag from the smaller to the larger and select aspect ratio,
then select one of the views and go to the size inspector on the
right bar
under Constraints, select the constraint that was just added, double
click it
under First Item select shorterView.Height
under Relation select Less Than or Equal
under Second Item select tallerView.Height
make sure Constant is 0 and Priority is 1000
set the Multiplier to 1:4 (I believe this is the correct ratio, but I always just mess around with these numbers until I'm satisfied with the views)
I'm currently using this in one of my projects, and it's working really well, so hopefully it will work for you, too!
(The top answer here might also help :] )

Resources