I am attempting to position two buttons in a view so that they are constrained to the middle guideline - that is, I want the space on the leading edge of Pause and the trailing edge of Skip to vary the same amount so the two buttons remain centered in the view.
I've been unable to find a way to specify the middle guideline for alignment or constraint. Sure I'm just missing something :-) The auto constraints don't do the right thing at all.
Thank you!
Align them to center X and then set the constraint's constant to some positive or negative offset.
Related
Let's say you have a label on the right and another label on the left of a screen. Text on the left one can grow till the end of the screen. Is it possible to make constraints to push the left label down if the right one grows so big it starts to compress the left one?
Ps. without constraints it seems easy, but the question is about constraints
Is it possible to make constraints to push the left label down if the right one grows so big it starts to compress the left one?
Yes, but not by magic. You would need, in code, to detect that this has happened and actually change the constraints entirely in order to rearrange the labels.
A more common solution to this problem is to give the left label a fixed minimum size so that the right one cannot compress it beyond a certain amount. You can even let the right label grow to multiple lines.
i have a TableViewCell and i want it to contain 2 labels both centered vertically in the cell, one at the left border and one at the right border(with respective text alignment). Now the right one should remain 1 line(its only a number with max 6 digits), but if the left one gets too big and would overlap the right one, i want it to make line breaks.
Now i've tried different approaches, for example i first added the right one and gave it the constraint "trailing space to superview = 0" and "center vertically in container".
Then i added the other one, gave it the constraints "leading space to superview = 0", "trailing space to rightLabel = 0" and "center vertically in container" and set lines to 0.
But that didn't work, it didnt make line breaks and overlapped the right label.
How can i achieve that?
You need to set the correct content-hugging and compression-resistance priorities (by default they have the same value), that's why the system doesn't know which constraint to break.
Try changing the content-hugging and compression-resistance priorities so that they are no longer equal. The system breaks the constraint having the lowest priority first.
Select the label. Open the Size inspector, find the Content Hugging Priority and Compression Resistance Priority settings. Change priorities.
I have 2 labels that are stacked and have a bottom space constraint to their common container. I want to hide the lower label if there's no value for it and move the upper label down to the lower label's position. I can do that easily with an outlet bound to the bottom space constraint for the upper label.
The problem here is that these labels have different font sizes. So, when I set the same value for the bottom space constraint for the upper label, like I have set for the lower one, the upper label does not end up at the same vertical position.
I assume this is because these labels have different descenders, due to different fontsizes. It would be easy to solve that if I could set the bottom space constraint not for the frame of the text, but its base line. I'd like to do this in IB, if possible.
I don't think it is possible to set a constraint to the strings baseline. Your comma is way bigger in your upper label, thats why the numbers don't sit at the bottom of the label. If you make the label smaller, you cut those commas of.
I see two solutions here:
Leave it that way.
Set the constant of your constraint to something lower than 0 (I guess -2 to -5) so that your numbers perfectly align with the smaller label.
I have several objects that are static in size that hug the top and bottom of the screen. However, there is a blank area in the middle of the screen that I would like to stretch with orientation change.
I have solved an issue by adding an extra clear view [paddedView] that can be stretched but wanted to know if there was an easier way to do this without the paddedView just with Visual Format Language.
#"V:|-20-[topLabel(40)]-15-[anotherTopLabel(40)]-[paddedView]-[bottomView(73)]|";
Instead of a view used only for padding, you can either specify a lower priority to a distance or specify the constraint as "greater than or equal to".
Since what you want is a flexible space, the last option sounds like the best one:
#"V:|-20-[topLabel(40)]-15-[anotherTopLabel(40)]-(>=15)-[bottomView(73)]|"
The number 15 is of course just an example.
I'm working on a game where you have an 8x12 grid where each cell is the same size and all the cells are directly next to one another.
You drag around various Tetris-like shapes and place them on the grid in valid locations, a valid location being one where all the cells that the shape will occupy are not occupied by some other shape.
My problem is that I'm not sure how to search the grid space for valid locations. I've been searching for an algorithm that will solve this sort of problem, but I have come up empty handed thus far. It seems like it should be pretty straightforward to detect valid locations, but I have not been able to come to a successful solution.
Any processes, algorithm suggestions, or ideas for how to go about solving this would be extremely helpful. Thanks!
EDIT:
Here is the expected functionality: When the shape is in a valid location, it can be dragged between valid locations freely and follows the mouse pointer. However, when you try to drag the shape into an invalid area (ie movement in the direction specified would place one or more blocks of the shape in invalid locations), it stays in the last valid location.
At this point, when the mouse is in an invalid area, I want to do some "predictive" movement so that if the player moves the mouse cursor near a valid position, the shape then "snaps" into place, say if the valid position is two grid spaces away.
Thanks for your suggestion so far; I hadn't thought of that method!
As described, the algorithm would be quite simple. Arbitrarily choose a starting block on your shape, and try to match it to each open cell of the grid. If "drawing" the shape with the block aligned with the current cell doesn't cause a collision, you've found a valid position.