开发者

From willAnimateRotationToInterfaceOrientation how can I get the size the view will be after rotation?

开发者 https://www.devze.com 2022-12-21 10:08 出处:网络
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?

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:

  1. Subclassing UIView and doing the layout you need inside -(void)layoutSubviews, or;
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号