当方向改变时,webview的大小也会改变,但并不平滑



我已经以编程方式创建了一个WebView。并在方向改变时改变它的大小。但是当我改变webview的大小时,它会改变,但不是很顺利。我怎样才能使它平滑地改变大小呢?

这是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44)];  //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen

    NSURL * urlStr = [NSURL URLWithString:@"https://www.google.co.in/"];
    [webView loadRequest:[NSURLRequest requestWithURL:urlStr]];
    [self.view addSubview:webView];
}

-(void)orientationChanged {

    UIDeviceOrientation   orientation = [UIDevice currentDevice].orientation;

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if(orientation == UIInterfaceOrientationLandscapeLeft)
    {
        if (screenBounds.size.height == 568)
        {
            webView.frame = CGRectMake(0, 44, 568, 230);
        }
        else
        {
            webView.frame = CGRectMake(0, 44, 480, 230);
        }
    }
    else if(orientation == UIInterfaceOrientationLandscapeRight)
    {
        if (screenBounds.size.height == 568)
        {
            webView.frame = CGRectMake(0, 44,568, 230);
        }
        else
        {
            webView.frame = CGRectMake(0, 44, 480, 230);
        }
    }
    else if(orientation == UIInterfaceOrientationPortrait)
    {
        //do change in design when ios change
        if (screenBounds.size.height == 568)
        {
                 webView.frame = CGRectMake(0, 44, 320, 524);
        }
        else
        {
                 webView.frame = CGRectMake(0, 44, 320, 400);
        }
    }
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        //Do what you want in Portrait Upside Down
    }
}

你可以使用动画。检查这些链接

http://www.raywenderlich.com/2454/how-to-use-uiview-animation-tutorialhttp://www.raywenderlich.com/5478/uiview-animation-tutorial-practical-recipeshttp://developer.apple.com/library/ios/文档/UIKit/引用/UIView_Class/

你可以使用block方法

animateWithDuration:delay:options:animations:completion:

或者您可以使用带有静态方法(类方法)的旧API

+ beginAnimations:context:

初版

[UIView animateWithDuration:1.0f animations:^{
    if (screenBounds.size.height == 568)
    {
        webView.frame = CGRectMake(0, 44,568, 230);
    }
    else
    {
        webView.frame = CGRectMake(0, 44, 480, 230);
    }
}];

或者如果你的目标是>= ios 6.0你可以使用NSLayoutConstraint

最新更新