代码之家  ›  专栏  ›  技术社区  ›  Filip Ekberg

仅允许在UIWebView上更改方向/旋转

ios
  •  2
  • Filip Ekberg  · 技术社区  · 14 年前

    我有一个 UIWebView 我用代码创建并使用 initWithFrame 定义启动大小。默认情况下,当我旋转设备时,不会旋转任何东西,并且方向与启动时相同。

    应用程序基于 UITabController 控制器中的所有选项卡都不显示 UIWebViews 为了 我没有 希望允许整个应用程序旋转。

    所以我有两个问题。

    • 是否可以只允许旋转uiWebView的内容?
    • 如果上述操作不可行,如何在旋转时移除选项卡(应用程序代理?)您是否需要刷新uiWebView的内容以跨窗口扩展它?
    1 回复  |  直到 14 年前
        1
  •  3
  •   leukosaima    14 年前

    - (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);
    }
    }