目前我有一个滚动视图,里面有一些图片。如果我将手机旋转到横向,我希望滚动视图中的图片变大(全屏),以便整个屏幕覆盖图像。将其旋转回肖像应删除全屏图像。
到目前为止,我在viewDidLoad中检测到旋转变化:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOrientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
并处理它:
- (void)handleOrientationChange
{
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
UIImageView *fullScreenImage = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[fullScreenImage setImage:[UIImage imageNamed:[[PflanzenSingletonClass sharedManager] loadStringFromUserDefaultsForKey:CurrentScrollViewImage]]];
[fullScreenImage setTag:999];
[self.view addSubview:fullScreenImage];
} else
{
[[[self.view subviews] objectAtIndex:([[self.view subviews] count] - 1)] removeFromSuperview];
}
}
但它没有按预期工作(当然是我的错)。
有什么想法吗?非常感谢。
答案 0 :(得分:0)
如果您在ViewController中处理此问题,则可以覆盖下一个方法,而不是使用通知:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)
要使ImageView全屏(并在导航栏上显示),您可以将其添加到keyWindow:
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
[imageView setFrame:keyWindow.bounds];
[keyWindow addSubview:imageView];
<强>更新强>
此外,您可以在单独的UIViewController模式中呈现UIImageView。 在这个ViewController实现方法
中- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation {
if (UIInterfaceOrientationIsPortrait(toOrientation)) {
[self dismissModalViewControllerAnimated:];
}
}
还要确保您的应用程序支持横向和纵向方向。它可以在摘要选项卡或App-Info.plist中编辑。也可以通过覆盖下一个方法来指定某些控制器:
for ios6:
- (BOOL)shouldAutorotate;
- supportedInterfaceOrientations;
for ios5:
- (BOOL)shouldAutorotateToInterfaceOrientation: