cannyboy
cannyboy

Reputation: 24406

Convert UIView co-ordinates from portrait to landscape and vice-versa

My iPad app allows the user to create UIViews and place them on the main view. So these views are created programmatically rather in xib files. It then remembers the co-ords of these UIViews. The user can then choose to show this collection of UIViews later.

----------------   ----------------
|Collection 1 >| > |          |   |  
|Collection 2  |   | View A   |   |
|Collection 3  |   |          |   |
|              |   |--------------|
|              |   |     |        |
|              |   |     | View B |
|              |   |     |        |
----------------   ----------------

So, above you can see that Collection 1 has been selected.

Now, the app should be usable in both landscape and portrait. So if a view is created in portrait, and then displayed in landscape how could I handle the co-ords? And vice-versa? And even when views are being created.

In my case I want UIViews to adjust proportionally, and if the views abut each other in one orientation, then they should also abut in the other orientation. So the above becomes this in landscape:

----------------   ------------------
|Collection 1 >| > |          |     |  
|Collection 2  |   | View A   |     |
|Collection 3  |   |----------------|  
|              |   |     | View B   |
|              |   |     |          |
|              |   ------------------  
|              |    
---------------- 

I guess part of what I'm asking is: Is there a way of obtaining what the co-ords would be in the other orientation without actually rotating?

Upvotes: 0

Views: 458

Answers (3)

Mert
Mert

Reputation: 6065

You just need to set autoresizing masks. Like view.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight

EDIT: I have just realized, I forgot about the case for view b.

For main view you need set : UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight

For view A : UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight

Since height of view A will be decreased, view B has to move up. We need to set flexible top margin mask.

For view B : UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight | UIViewAutoresizingFlexibleTopMargin

While giving that autoresizing masks I assumed you placed the views top relative. It means you have set frames either just a value like 10.0 or relative to element of which is placed above the view it selfs, like viewB.frame = CGRectMake(xCoordinate, viewA.frame.origin.y + viewA.frame.size.height, width, height)

It wont work if you have them bottom relative like viewB.frame = CGRectMake(xCoordinate, mainView.frame.size.height - 200.0, width, height)

Upvotes: 1

morningstar
morningstar

Reputation: 9122

You should turn "on" all the resizing masks, for the subviews (View A and View B).

viewA.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
                         UIViewAutoresizingFlexibleWidth |
                         UIViewAutoresizingFlexibleRightMargin |
                         UIViewAutoresizingFlexibleTopMargin |
                         UIViewAutoresizingFlexibleHeight |
                         UIViewAutoresizingFlexibleBottomMargin;

If you do that, then all the edges of the views will keep their location as a proportion of the parent view's space. E.g., View A's frame can be thought of as (left, top, width, height) = (0%, 0%, 80%, 50%) and View B's as (40%, 50%, 60%, 50%). When you rotate the views' bottom and top are still at 50%, though that's numerically a different value. View A's left and top still abut the edge of the parent view. Even though those margins are flexible, they won't change if the margin is zero. Same for B's right and bottom edges.

Upvotes: 1

Evgeny S
Evgeny S

Reputation: 82

You can move out the logic to deal with coords in a separate method and call this method in usingShouldAutorotateToInterfaceOrientation or shouldAutorotate dependently on ios version

Upvotes: 0

Related Questions