我正在尝试创建一个 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 最初处于横向模式并且我显示我的组件时,它以纵向模式显示。
你将如何做到这一点?