How to deal with iOS UIViews rotation and alignment - ios

In my existing app, I had 5 of my UIView derivatives (labels and buttons) laid up horizontally, i.e. on 0 degrees. They were all aligned on their left edges (so that button1.frame.origin.x = button2.frame.origin.x and so on.)
Now, for update purpose, I need to rotate all 5 of them, by 9 degrees anticlockwise.
I achieve the the rotation easily.
But I can't get how adjust their left edges so that they would all be aligned again after rotation.
The requirement, in summary, is this: The first control need to remain fixed (acting as pivot point) - the 4 others need to move right in order to maintain left alignment after rotation.
I tried putting incremental x value to each of the last 4 controls (keeping the pivot x fixed) but so far don't achieve exact alignment.
After rotation, it looks like all of them are center aligned, instead of left aligned which I really want.
I know what I really want, but just looking for a smarter way so that it won't be ugly like it is now.

Read about layout constraints. That will really help you in many ways. In layout constraints the concept is very simple like
Control1.attribute = Control2.attribute + C
So you can literally program each controls attributes and achieve a fine grain control. You can then code it such that all the controls X is same as other controls X, they are all same distance from another control etc.
You can have multiple layout constraints and the system will try to satisfy as many as possible.
But the only thing is that this is possible in iOS 6.0.

Related

Setting up variable constrains in xcode

I'm having trouble setting up constraints.
This is the thing I am trying to achieve:
I have 2 objects A and B. I want there there to be a maximum of 100 points between A and B if its a larger screen, or less if its a smaller screen.
Also, I want there to be a minimum of a 20 point margin from the main view controller. So on a smaller screen the minimum spacing will be 20 points, and on a larger screen the margin can be as wide as needs in order to satisfy the space between A and B at 100 points.
To illustrate how I want it to look on various screens:
4 inch screen
I want there to be a 20 point margin on either side and the middle (space between A and B) can be variable, i.e. 100 or less.
5.5 inch screen
The margins can be variable and expand as large as they need to, but the middle section (space between A and B) can be a maximum of 100.
Any help how to achieve this would be greatly appreciated.
This can be achieved with help from three dummy views and two width constrains with different priorities. I set up a test project to test my thought. check it out to see whether it's what you needed
https://github.com/dopcn/testSeg

Image position between inputs xcode 6

Good afternoon,
I'm trying to put an image in the middle of two inputs, that means that I would like to have the same margin to the first input as the second input. I'm going to explain better with an example:
[input a]---- 20px-----[image]---- 20px-----[input b]
I want to have the image always at the middle between them, because when I rotate the screen I have to make it large.
I have to use constraints? Which one? Is it possible?
Thanks in advance.
There are a number of ways to approach this. (There are a number of ways to approach everything, lol).
I'm only going to show AutoLayout solutions because it will make things easer when things are moving. Also, your description is pretty much already using AutoLayout Visual Format Language.
The first would be to place inputA, inputB and image into a third view which I'll call interfaceView.
Now you can lay it out like this...
|[inputA] // i.e. up against the left edge
[inputB]| // i.e. up against the right edge
[image] is different. You then align the centre of image with the centerX of interfaceView
The second option is to use "spacer" views.
Create two more views called spacer1 and spacer2 and layout the screen like this...
[inputA][spacer1][image][spacer2(==spacer1)][inputB]
Make spacer 1 and 2 hidden. They are just there for layout purposes. Now, because I have given them equal widths they will align the image view exactly in the middle of A and B.

Solid solution to flexibly resize UIView based on subviews

Imagine you have four or so views, all width 100, different heights. You have a wrapper view W which holds them all.
A |
B | W
C |
D |
the heights of the small views can change. At that time you want them all to move, float, appropriately, and resize W.
Now, I was just about to write a few lines of code to do this.
So .. (1) you'd have W find all the subviews and list them in order from top to bottom. Then (2) each time there is a change, you'd (3) reposition each of ABCD. the position of each one is the sum of the heights of the items above it, and (4) resize W to the sum of all heights.
Now that's all fine but -- idiots reinvent the wheel!
Am I missing something obvious in iOS? is there already a package everyone uses to do this all the time? Or something built in? What's the situation?
(Note that of course frustratingly, for our Android friends this is built in! And of course any web-html system does this automatically.)
What's the right engineering solution for iOS views here? For the record this is iOS7+ only, no old-fashioned stuffs need be covered, if it makes a difference. Cheers
(1) you'd have W find all the subviews and list them in order from top
to bottom. Then (2) each time there is a change, you'd (3) reposition
each of ABCD. the position of each one is the sum of the heights of
the items above it, and (4) resize W to the sum of all heights.
You can use constraints in Interface Builder for that whole process, no code required. Do this:
set the width of subview A to 100
constrain B, C, and D to match A's width
add vertical spacing constraints between A and B, B and C, and C and D to maintain their relative position
add a vertical spacing constraint between W (the superview, shown in gray) and A
add a vertical spacing constraint between W and D
add leading and trailing space constraints between W and view A
You'll end up with something that looks like this:
The constraints editor in Xcode isn't completely intuitive, but it is easy to use once you understand what you can and can't do with constraints in IB and when you need to use code to set up the constraints.

Spacing In VLayout

I am working inside a Tabpane and I want to have 2 buttons on the bottom-right of the tap-pane, so I thought I just add a LayoutSpacer, but this resulted in
but I wanted it to look like
How can I make my Layoutspacer "bigger"?
In Smart GWT there are different methods to set the size of the component's vertical dimension.
Try with any one
canvas.setHeight100()
canvas.setHeight("100%")
canvas.setHeight("*")
Layouts may specially interpret percentage sizes on their children, and also allow "*" as a size.

What is the meaning of "topmost" in the alMostTop alignment in TAlignLayout?

In the documentation for TAlignLayout, a type which addresses visual alignment settings for FireMonkey controls, I'm trying to figure out the difference between the alTop and alMostTop constants, the only difference is that the definition of alMostTop contains the phrase "set to be the topmost".
(The same goes for the constants alMostLeft, alMostRight and alMostBottom, which are distinguished from alLeft, alRight and alBottom with similar phrases using the words
"leftmost", "rightmost" and "bottommost".)
I don't know what "set to be the topmost" means, and none of the guesses I make seem to fit with observed FireMonkey behaviour:
If it means displaying that control on top (at the front of the display) relative to its siblings, then what does leftmost mean?
If "leftmost" means it is the sibling shown furthest to the left, out of all siblings that have a alLeft alignment, then why does the CustomListBox demo use a layout with two elements that have a alMostLeft alignment? How does it decide to place the two elements that particular way round?
The Embarcadero forums don't shed any light on this either.
[Update]
To be clear, experiments do indeed suggest that with two components aligned to the top, you can choose which one is to be further up the screen by setting that one to have alMostTop, and the other to alTop.
However, I thought of another possibility: suppose you have two child components, one to be aligned at the left, another aligned at the top. Then they will fight for ownership of the top-left corner. Experiments seem to suggest that some influence can be obtained by choosing between alTop and alMostLeft alignments for the two controls vs alMostTop and alLeft alignments. I can't see exactly what is going on here, and it's not clear in the documentation. It's also not clear what should/might happen if two child controls are both set to alMostTop.
When you have 2 (or more) controls aligned to top, the alMostTop will be on the top of all others. Same wise with alMostRight, alMostLeft, and alMostBottom.

Resources