根据方向正确显示子视图控制器

时间:2013-03-05 14:45:06

标签: ios uiviewcontroller uiinterfaceorientation

我正在尝试创建一个facebook组件(UIViewController的子类)来共享图片。现在可以在这里找到其状态的演示: http://jeremy.gabriele.free.fr/SO/rotatesubvc/

在ViewController中集成,我想用这种方式使用它:

_shareSheet = [[FBSharePictureViewController alloc] initWithImage:self.currentImage.image andDelegate:self]; // Create the instance
[_shareSheet showWithinViewController:self]; // Actually display the sheet with animation
[_shareSheet hide]; // When needed, hide with animation

视图架构:

- [UIView] view
-- [UIView] overlay, a full-screen black view (when showWithinViewController: is called, slightly fade in to alpha 0.8)
-- [UIView] _sheetViewContainer, contains the sheet
---- [UIView] topView, contains the cancel and post buttons + a centered label
---- [UIView] bottomView, contains the picture we will share + an UITextView the user can enter text in.
-- spinner, an UIActivityIndicatorView

showWithinViewController函数:

- (void)showWithinViewController:(UIViewController *)parentViewController {

  [parentViewController addChildViewController:self]; // So the parent will foward actions like rotation,...

  // I want it to match the full size of my screen, so if we have a transparent
  // status bar the overlay will be visible under the status bar too
  CGRect frameBefore = self.view.frame;
  [parentViewController.view addSubview:self.view]; // view frame = {{0, 0}, {320, 460}}
  self.view.frame = frameBefore; // view frame = {{0, -20}, {320, 480}}

  self.view.hidden = NO;

  [UIView animateWithDuration:0.4 animations:^{
    // _sheetViewContainer.frame is originally centered horizontally and y is offscreen
    _sheetViewContainer.frame = [self getSheetViewFinalPos]; // I want the sheet to be centered between the UINavigationBar and the UIKeyboard
    self.overlay.alpha = 0.8f;
  } completion:^(BOOL finished) {
    self.cancelButton.enabled = YES;
    self.postButton.enabled = YES;
  }];

}

getSheetViewFinalPos函数:

- (CGRect)getSheetViewFinalPos {
  CGFloat keyboardHeight = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 216 : 162;
  CGFloat navBarHeight = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 44 : 28;

  CGRect f = _sheetViewContainer.frame;
  f.origin.y = navBarHeight + 20 + (self.view.frame.size.height-20-navBarHeight-_sheetViewContainer.frame.size.height-keyboardHeight)/2;
  return f;
}

现在我的问题:(如果您访问了以上链接,可以进行预览:))

我想找到我的组件根据方向自动调整框架大小的最佳方法。

  • 我已经尝试了UIAutoresizingMask,但它有非常奇怪的行为,我无法成功实现它:/。
  • 我尝试使用viewWillLayoutSubviews方法,但如果我的视图被隐藏(当我旋转并且视图未显示时),这不起作用。
  • 我尝试使用didRotateFromInterfaceOrientation但是当父ViewController最初处于横向模式并且我显示我的组件时,它以纵向模式显示。

你将如何做到这一点?

0 个答案:

没有答案