0

我以编程方式创建了一个 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
    }
}
4

1 回答 1

0

您可以使用动画。签出这些链接

http://www.raywenderlich.com/2454/how-to-use-uiview-animation-tutorial http://www.raywenderlich.com/5478/uiview-animation-tutorial-practical-recipes http://developer。 apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/

您可以使用带有方法的块

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

于 2013-04-09T07:41:27.847 回答