I am trying to resize the objects in a UIView when the device is rotated without hard coding the width and height. In this code how would I get the newWidth
and newHeight
?
- (void)willAnimateRo开发者_运维百科tationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
child.frame = CGRectMake(10, 10, newWidth - 20, newHeight - 20);
}
This would be about right.
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
// we grab the screen frame first off; these are always
// in portrait mode
CGRect bounds = [[UIScreen mainScreen] applicationFrame];
CGSize size = bounds.size;
// let's figure out if width/height must be swapped
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
// we're going to landscape, which means we gotta swap them
size.width = bounds.size.height;
size.height = bounds.size.width;
}
// size is now the width and height that we will have after the rotation
NSLog(@"size: w:%f h:%f", size.width, size.height);
}
If possible, you're better either:
- Subclassing
UIView
and doing the layout you need inside-(void)layoutSubviews
, or; - Making use of
autoresizingMask
to automatically layout your views.
It might be best to create a separate view altogether and call that view in the did rotate method.
You can set the Autosizing properties of the view from the interface builder. That will resize your view when rotation happens. But there may be problem that some of the views might not fit properly.
精彩评论