代码之家  ›  专栏  ›  技术社区  ›  Nathan de Vries

UINavigationController自动旋转到横向不会更新UIView帧

  •  3
  • Nathan de Vries  · 技术社区  · 17 年前

    我正在开发一个完全以横向模式运行的应用程序(UIStatusBarHidden=YES和UIInterfaceOrientation=uiInterfaceOrientationAndScapeRight)。我使用的是NavigationController,rootViewController(MainViewController)设置如下:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    

    - (void)loadView {
      CGRect frame = [[UIScreen mainScreen] applicationFrame]; // This returns a portrait frame
      MainView *view = [[MainView alloc] initWithFrame:frame];
      self.view = view;
      [view release];
    }
    

    然后在MainView中,我加载如下子视图:

    - (id)initWithFrame:(CGRect)frame {
      if (self = [super initWithFrame:frame]) {
        self.pdfView = [[PDFView alloc] initWithFrame:frame];
        [self addSubview:pdfView];
      }
       returnself;
    }
    

    我的问题是,MainView在横向模式下正确渲染(尽管传递到[MainView initWithFrame:]的帧是纵向的),而子PDFView渲染到纵向帧。我还尝试在[UIView initWithFrame:]实现中应用转换,如下所示:

    view.center = CGPointMake(view.frame.size.height/2.0, view.frame.size.width/2.0);
    view.transform = CGAffineTransformRotate(view.transform, (M_PI/2.0));
    

    但这似乎不起作用,因为帧值不正确。在某些情况下,忽略框架并手动创建这样的框架似乎是可行的:

    CGRectMake(0.0, 0.0, 480.0, 320.0)
    

    但很明显,像这样的手动调整并不理想。

    有什么想法吗?

    3 回复  |  直到 17 年前
        1
  •  1
  •   Ryan Ryan    17 年前

    我不知道这对您的情况是否有帮助,但请确保所有视图和控件都支持自动旋转。控制者也没有。如果选项卡栏控制器的任何子视图不支持自动旋转,则选项卡栏控制器将停止支持它。我必须创建自己的UINavigationController并使其从UINavigationController继承,然后实现自动旋转。由于您完全是在横向环境中运行应用程序,我不知道这是否有帮助,但它可能会让您开始找到解决方案。

        2
  •  0
  •   Robert Redmond    16 年前

    我在地图视图中遇到了类似的问题。基本上,您必须确保所有子视图实现:

        3
  •  0
  •   Community Mohan Dere    9 年前

    因为主屏幕总是 肖像 Height and width on iPhone (/iPad)