iOS:在横向模式下无法在模式视图中调整帧大小
我有包含UICollectionView的UIViewController。单击任意UICollectionViewCell,我会在模式视图中显示该单元格的详细内容。我通过故事板在模态视图中添加了对象(UILabel、UIImage等)。当使用NSNotificationCenter更改设备方向时,我将调整帧的大小: 在模态控制器的viewDidLoad方法中:iOS:在横向模式下无法在模式视图中调整帧大小,ios,iphone,objective-c,xcode5,Ios,Iphone,Objective C,Xcode5,我有包含UICollectionView的UIViewController。单击任意UICollectionViewCell,我会在模式视图中显示该单元格的详细内容。我通过故事板在模态视图中添加了对象(UILabel、UIImage等)。当使用NSNotificationCenter更改设备方向时,我将调整帧的大小: 在模态控制器的viewDidLoad方法中: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotificat
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter* notifCenter = [NSNotificationCenter defaultCenter];
[notifCenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
方向更改内容:
-(void) orientationChanged {
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
self.productImage.frame = CGRectMake(61, 5, 124, 138);
self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
self.productName.frame = CGRectMake(220, 5, 150, 102);
self.productPrice.frame = CGRectMake(220, 120, 124, 21);
}
else {
self.productImage.frame = CGRectMake(61, 5, 124, 138);
self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
self.productName.frame = CGRectMake(20, 244, 207, 102);
self.productPrice.frame = CGRectMake(61, 354, 124, 21);
}
}
现在,当我在纵向模式下启动应用程序并单击任意UICollectionViewCell时,模式视图将正确显示,现在,当我切换到横向模式(而不忽略模式视图)时,由于上述方法,我能够调整帧的大小
问题是当我以横向模式启动应用程序,然后单击UICollectionViewCell时,模式视图将以纵向模式显示。我希望它是在横向模式与正确调整大小的帧。我尝试在viewDidLayoutSubviews中设置帧&在ViewDidDisplay中设置帧,但没有用
ViewDidLayoutSubView的内容:
-(void)viewDidLayoutSubviews{
self.productImage.autoresizingMask = 0;
self.productPrice.autoresizingMask = 0;
self.productName.autoresizingMask = 0;
self.siteLogo.autoresizingMask = 0;
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
self.productImage.frame = CGRectMake(61, 5, 124, 138);
self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
self.productName.frame = CGRectMake(220, 5, 150, 102);
self.productPrice.frame = CGRectMake(220, 120, 124, 21);
}
else {
self.productImage.frame = CGRectMake(61, 5, 124, 138);
self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
self.productName.frame = CGRectMake(20, 244, 207, 102);
self.productPrice.frame = CGRectMake(61, 354, 124, 21);
}
}
提前感谢。根据
模态视图控制器在iOS 6中不再获得旋转调用:willRotateToInterfaceOrientation:duration:,WillAnimateRotationInterfaceOrientation:duration:,和didRotateFromInterfaceOrientation:方法不再在任何在其自身上进行全屏演示的视图控制器上调用,例如使用:presentViewController:animated:completion:调用的那些方法
可以让显示模态视图控制器的视图控制器通知其旋转。另外,现在使用:presentViewController:animated:completion:显示视图控制器。presentModalViewController:animated:不推荐在代码中使用
编写以下代码:在模态视图控制器中:
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0){
return UIInterfaceOrientationLandscapeLeft;
}
MyVC *webVC = [[MyVC alloc] init];
[self.navigationController presentViewController:myVC animated:YES completion:^{
[myVC shouldAutorotate];
}];
在调用视图控制器时:
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0){
return UIInterfaceOrientationLandscapeLeft;
}
MyVC *webVC = [[MyVC alloc] init];
[self.navigationController presentViewController:myVC animated:YES completion:^{
[myVC shouldAutorotate];
}];
您是否尝试过使用
willRotateToInterfaceOrientation:duration:
@AbhishekBedi是的,我尝试过,但结果仍然相同。您是否将didAutorotateToInterfaceOrientation
方法设置为是。请张贴完整的班级代码。希望您调用的方法如下:`[self.navigationController presentViewController:webVC动画:是完成:^{}]`同时检查:我在UICollectionView的didSelectItemAtIndexPath方法中添加了两个控制器之间的模式segue和调用performSegue