curtisdf
curtisdf

Reputation: 4210

Get dimensions an autorotated UIView will be given after screen is rotated?

The question: Is it possible to obtain the size that an autorotated UIView will be given after the screen is rotated, but to obtain this value before the rotation is completed? If so, how?

Background: I'm building an iPad app that has a screen where several UILabels are arranged in columns within a containing UIView (which is smaller than the screen's main UIView.) The number of labels and the length of text in each label is not known during compile-time. They are layed out on the screen dynamically at runtime with an algorithm that tries to choose the optimum number of columns and widths that keeps things from being clipped off the screen. To make the rejiggering process appear as smooth as possible to the user, I believe I need to do this inside of:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

But of course this method is called before the rotation is completed and hence the need to know in advance the size that the containing UIView will soon have.

Upvotes: 1

Views: 975

Answers (2)

Rob
Rob

Reputation: 437552

In iOS 5, viewWillLayoutSubviews will achieve what you're looking for.

In iOS 4, you can make the moves a little less jarring by animating the moving of your controls/subviews, even though it takes place in didRotateFromInterfaceOrientation (I know it doesn't sound like it, but it makes a huge difference). Thus, if you want to animate the move of a button to newFrame, it's as simple as:

[UIView animateWithDuration:0.25 animations:^{ 
    [button setFrame:newFrame]; 
}];

Finally, judicious use of autoResizingMask, helps immensely, too, though I gather that this is not a possibility given that you are moving controls around in respect to each other.

Upvotes: 1

Stefan H
Stefan H

Reputation: 6683

Check out:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

You can find all those methods here:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Upvotes: 0

Related Questions