Center multiple UILabels on a line - ios

I want to horizontally center multiple UILabels - as a group - on a line in Interface Builder. One the straight view controller i could not figure out how to do this. I read comments about place the UILabels in a View and then centering the view in the view controller. When I tried this, overtime I said to update frames in the interface builder, the View would be resized down to nothing. (i.e. its height and width would be set to 0 by IB). I need to know how to get this to work in interface builder.
An example of a line containing multiple labels I want horizontally centered is below. The braces simply indicate the start and end of each label and are not art of the text. The <- 6 -> is meant to indicate the trailing space from label 1 to label 2 is 6
Label 1 Label 2
[Rating: 0.0]<- 6 ->[Distance: 125.34 Kilometers]
Any suggestions would be greatly appreciated.
Here is an EXACT example of what I have done and it does not work:
Create a new view controller in IB
Place a page label at the top of the page centered horizontally and aligned to the top of the layout guide.
Add a view with the following constraints on the page:
Now add two labels to the view with the following constraints:
Once this is done I get the following error and updating the frames will cause it to be give a height and width of 0
Here is what the page looks like in IB:
I need the height and width of the view to size automatically so that I have put a multiline label in the view and have all the contents of the view treated as a group and centered on the page.
Any suggestions would be greatly appreciated.

Here is a snapshot of a working set of all the constraints on an abbreviated layout that produces an always-centered view that automatically resizes with the child views (i.e., as the content of any label changes, the view grows or shrinks around it).
Important to your solution, the view has neither a height nor width constraint; its size is constrained entirely by its descendant constraints.
The key is that every component has a direct or indirect constraint from which its size and position are specified or can be inferred. For example, in order for the view to infer its width, the child labels must have a leading space constraint on the first label, a trailing space constraint on the last label, and a horizontal space constraint between interior labels. Those constraints plus the contents of the labels allow the width of the view to be inferred ... and force the view to dynamically conform to that width.
The same applies for the view's height. For example, you can specify the top and bottom space of just one label, or all of them. If just one, the remaining labels can be vertically aligned with it (see "Align Center Y to: Label1" in the screenshot).

Related

Dynamic width view with subviews

I am trying to create the following in a xib:
https://imgur.com/nyLv65t
Please ignore the poor drawing. Basically, the black border is the main view. Inside that main view, I have a smaller view (in blue) that should be on the right side of the main view, centered vertically. The blue view also has a label as a subview, so I need the blue view to have a dynamic width based on the length of the label.
However, I can't seem to get the constraints right. Every time, I get red constraints and errors. What would the correct constraints for this be?
You need to give below three constraints
Vertically center to it superview
Constant trailing space.
Leading constrain with >= value to specify the value of x
Storyboard constraints:
Download demo code

How to use auto layout to resize views in a table view cell?

I have a cell in which I place four buttons and four labels. Each button gets assigned a picture with width 50 and height 50. Furthermore, all buttons have a corresponding label describing what they're intended for.
My objective is to have the buttons and labels resize to keep the buttons' and labels' aspect ration intact while the screen dimension changes on different devices. I have been playing with auto layout changing the hugging and compression to achieve this but haven't been successful yet. Any help would be much appreciated...
I think you should take a look at a UIStackView, because this seems exactly as a use case for stack. Just put each pair button/label in a stack, and then all four pairs into a horizontal stack, which you constraint to the cell itself. You should be able to handle all you need just by configuring the stack’s properties (axis, distribution, alignment, spacing).
Embed your button and label into a view. Set the width of this view equal widths to content view and change the multiplier value to 1:4. This will adjust the widths of the views according to superview. Also, set the top and bottom constraint to 0 for this view.
Provide center align y-axis constraint to button after setting the width and height constraint to 50. Set its top constraint to a value you deem fit.
Set labels's leading and trailing constraint to a value like 8. Choose center alignment for text. Also, provide top constraint to buttona nd bottom to its superview.
Copy the view and paste to create the three views and provide them equal widths constraint to the first view. Also, provide their leading, trailing, top and bottom constraints.
Here are a fast tutorial in how to achieve that:
1-
2- completion of the first Gif:
Note you can achieve the same output using a UIStackView

Q: Auto Layout 4 squares Issue/General Auto Layout Issue

So I'm just trying to wrap my head around Auto Layout; I understand how to use constraints, just not how to apply them appropriately. In the picture below I had set no constraints and simply want this simple design to resize correctly on all size classes (Horizontally and Vertically).
http://tinypic.com/r/2ymxbop/9
What constraints would I need to put in to have these squares resize appropriately on all screens (horizontal/vertical) and can you do this in the ( W:Any H:Any ) size class or do you have to set constraints for each individual size class?
Thanks!
One size class is all you need in this case.
Set a constraint for each of the 4 gaps between the rects (i.e. the space between the top 2, between the top-left and bottom-left, ...).
Set a constraint for each rect to its 2 closest outer edges.
Now you've set four constraints for each rect - and that's all you need!
For that case: follow the steps
1.Add four uiview to screen, two top and two bottom.
2.In your case the view not touching to bottom layout guide,so you have to give fixed height.
Start the add constraints to views.
All views are correct width and height.
3.So take first view from left top,give leading space ,top space and adjecent spacing to right top view. And select the pin button from the canvas below right side option availabe, select height ,give how much height you need.
4.Now select right top view ,give constraint to top layout guide and trailing space.and also same height what you selected before for view one.
5.Now view three ,left bottom view , give leading ,spacing to top view and right spacing to fourth view and select same height .
6.Now fourth view, give spacing to top view ,trailing to container and select height.
Almost completed.
7.Select all views at once ,give equal withs(option available in pin).
lastly some warning will shows ,do update frames.

How To Properly Set Constraints To Multiple Buttons With Same Size To Prevent "Cannot find an outgoing row head for incoming head"

The layout you can see below is a UIViewController having four buttons with same size aligned horizontally and a UIPageViewController below them. The UIPageViewController holds 4 pages. Each page contains a UITableView.
The four buttons is embedded in a UIView in which the constraints I set is:
leading to super view
top to TopLayoutGuide.bottom
trailing to super view
aspect ratio (height equal to width 5:32)
The constraints of the buttons inside are like:
every button has constraint to top and bottom space to super view
first and last has constraint to leading and trailing of super view
every button's width is equal to the first
The problem is, sometimes when this view controller transitions to another view controller, the app will crash. The error would be "Cannot find an outgoing row head for incoming head". When I check the memory address of which control produced the crash, it was always the first button which has a title "Live Radio".
The problem would be solved if I set the height of the container view(view that embeds the four buttons) to a fixed value, not aspect ratio. However, I want the size of the tabs to expand with respect to the size of the screen. So, this won't be a good solution for me.
Is there any other way to make the four buttons expand proportionally with the screen without using aspect ratio?
Thanks!
-------- EDIT ---------
Regarding how I transition to another view controller, it's just plain
[self performSegueWithIdentifer:#"showArtistDetailsView"];
I got this problem fixed by interchanging the width and height of the aspect ratio constraint of the "Live Radio" button.

Using Autolayout adjust vertical position from horizontal center if too high

In the diagram I need to center align the right view with the left view. If the right view is larger than the left view I need to top align it. Can I do this with constraints (storyboard) or do I need to do something in code to adjust the position (via a constraint) of the right view after the size has been determined? The Right view has multiple UILabels that can grow so I don't know the size until runtime. The container is actually a UITableViewCell.
Centering is easy. The right view resizes and the cell adjusts to that size. The issue is the requirement to top align if its too big.
You want this:
You can do it with two constraints.
You want the top of the green view to be greater than or equal to the top of the pink view, always. That is, you require that green.top ≥ pink.top.
In addition, you want the Y-center of the green view to be equal to the Y-center of the pink view, when possible. That is, you prefer that green.centerY == pink.centerY, if it doesn't violate the other constraint.
Here's the first constraint, in the storyboard editor:
And here's the second constraint:
Note that I have lowered the second constraint's priority to 800, which means it's not required but it's strongly preferred.
You don't need to write any code to make this work. I did write code in my demo to wire up the slider and the label, but I didn't have to modify those two constraints from code.
In the storyboard, align the right view to the left view top.
Give the value as
topAlignCons.value= +(leftView.height/2) //in storyboard
so that it gets align to the center of left view
Create an outlet for the top align constraint in your view controller.
When the right view is larger that left view, change the outlet constraint value to 0.
topAlignConstraint.constant=#0;
Now it will be aligned to the left view top with no negative value.

Resources