我有一个视图控制器,我正在尝试以模式呈现。我需要将其移动到屏幕上的特定位置。下面的代码在纵向模式下工作正常,但在横向中,无论我将中心设置为哪个点,视图都不会重新定位。如果我使用 pcvc.landscapeView.center 而不是 pcvc.landscapeView,视图会重新定位,但灰色框仍保留在视图的原始位置,并且我无法与视图交互。
在横向时,我需要如何以不同的方式对待视图控制器?
PostCommentViewController *pcvc = [[PostCommentViewController alloc] initWithNibName:@"PostCommentViewController" bundle:nil];
pcvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
pcvc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:(UIViewController *)pcvc animated:YES completion:nil];
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
pcvc.portraitView.superview.frame = CGRectMake(0, 0, 750, 143);
pcvc.portraitView.superview.center = CGPointMake(384, 680);
}
else
{
pcvc.landscapeView.superview.frame = CGRectMake(0, 0, 683, 300);
pcvc.landscapeView.superview.center = CGPointMake(500, 60);
}
我真的不能帮助这一小段代码。加载名为 PostCommentViewController
的 xib 文件该 xib 文件有 2 个视图portraitView
和landscapeView
您尝试移动超级视图,以便这是包含portraitView
和landscapeView
的视图。
我不明白,但作为快速解决方案,您可以针对不同的布局制作不同的 xib。同样,我没有您的视图控制器层次结构,因此很难提供帮助。
在 didrotate 中调用此内容
-(void)changeOrientation {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
[[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
}
else {
[[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
}
}