开发者

Allowing orientation / rotation change only on a UIWebView

I've got a UIWebView that I've created with code and I am using initWithFrame to define the startup size. As default when I rotate the device nothing is rotated and the orientation is the same as when it started.

The application is based on a UITabController and all Tabs in the controller are not displaying UIWebViews and there for I do not want to allow the whole app to rotate.

So I got two questions.

  • Is it possible to allow only the content of a UIWebView to be rotated?
  • If the above is not possible, How do you remove the TabBar when rotated ( app delegate? ) and do you need to refresh the content of the UIWebView to span it accross the window?
  • 开发者_JS百科


I ran into a similar problem when I wanted to show an image fullscreen in landscape mode, and it's default position in portrait mode. Since my app contained a TabBarController with each of the tabs displaying a navigation controller, I had to move things around myself using "willAnimateRotationToInterfaceOrientation" for the view that contained the image.

In my app, the tab bar controller will display in all orientations except portrait upside down. If you lock the tab bar controller to one orientation, i believe you lock all subsequent views within the tab bar as well. Essentially i hid the status bar, the navigation bar and the tab bar whilst moving the navigation bar up out of view, and the tab bar down out of view, and resizing the content in the middle. Here's an example of what i did, supposing that you have self.WebView (that would have been my image, for example):

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {

float navBarHeight = self.navigationController.navigationBar.frame.size.height;
float tabBarHeight = ((UITabBarController *)self.navigationController.parentViewController).tabBar.frame.size.height;

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    float statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.navigationController.navigationBar.hidden = YES;
    ((UITabBarController *)self.navigationController.parentViewController).tabBar.hidden = YES;

    // TabBarController adjustments
    self.navigationController.parentViewController.view.bounds = CGRectMake(0, -tabBarHeight/2, 480, 320 + tabBarHeight);

    // Adjust view
    self.view.frame = CGRectMake(0, -statusBarHeight - navBarHeight, 480, 320);

    // Adjust web view
    self.WebView.frame = CGRectMake(26, 0, 426.6667, 320);
}

if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    float statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    self.navigationController.navigationBar.hidden = NO;
    ((UITabBarController *)self.navigationController.parentViewController).tabBar.hidden = NO;

    // TabBarController adjustments
    self.navigationController.parentViewController.view.bounds = CGRectMake(0, 0, 320, 480);

    // NavigationController adjustments
    self.navigationController.navigationBar.frame = CGRectMake(0, statusBarHeight, 320, navBarHeight);

    // Adjust view
    self.view.frame = CGRectMake(0, statusBarHeight + navBarHeight, 320, 480 - statusBarHeight - navBarHeight - tabBarHeight);

    // Adjust web view
    self.WebView.frame = CGRectMake(0, 0, 320, 240);
}
}

I believe by resizing your webview it should automatically fit the content within appropriately without having to "refresh" per se.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜