我有一个UIViewController
我用它来控制“弹出”视图,以便在整个应用程序中查看图像。它支持自动旋转,因为无论方向如何,它都会自动调整图像大小以正确适合。这工作得很好,但只是在我第一次初始化并显示视图控制器时。当它关闭时,我将删除UIView
从我的视图层次结构中释放视图控制器 - 但下次我实例化并将其添加到我的视图层次结构中时,它会停止接收-shouldAutorotateToInterfaceOrientation
旋转手机时发送消息。
这就是我实例化和显示它的方式:
popupVC = [[PopupVC alloc] init];
[popupVC viewWillAppear:NO];
[[[UIApplication sharedApplication] keyWindow] addSubview:popupVC.view];
[popupVC viewDidAppear:NO];
这就是我在完成后删除/释放它的方式:
[popupVC viewWillDisappear:NO];
[popupVC.view removeFromSuperview];
[popupVC viewDidDisappear:NO];
[popupVC release];
popupVC = nil;
我尝试过循环[[UIApplication sharedApplication] keyWindow]
子视图以查看我的弹出视图是否不在顶部,但它总是在顶部。而且它每次都有不同的地址,所以我确实知道它是视图控制器类的不同实例。
根据要求,这里是完整的loadView
方法来自PopupVC
:
- (void)loadView {
UIView *myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
myView.backgroundColor = self.overlayColor;
myView.autoresizesSubviews = NO;
myView.hidden = YES;
myView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view = myView;
[myView release];
_isVisible = NO;
UIView *myMaskView = [[UIView alloc] initWithFrame:self.view.bounds];
myMaskView.backgroundColor = [UIColor clearColor];
myMaskView.clipsToBounds = YES;
myMaskView.hidden = YES;
myMaskView.autoresizesSubviews = NO;
myMaskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myMaskView];
self.imageMaskView = myMaskView;
[myMaskView release];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
myImageView.center = self.view.center;
myImageView.hidden = NO;
myImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleHeight;
[self.imageMaskView addSubview:myImageView];
self.imageView = myImageView;
[myImageView release];
UIButton *myImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
myImageButton.frame = self.view.frame;
myImageButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleHeight;
[myImageButton addTarget:self action:@selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];
[self.imageMaskView addSubview:myImageButton];
self.imageButton = myImageButton;
UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
myActivityView.hidden = YES;
[self.view addSubview:myActivityView];
myActivityView.center = self.view.center;
self.activityView = myActivityView;
[myActivityView release];
}
The shouldAutorotateToInterfaceOrientation
handler 不是自定义代码响应旋转事件的地方。它的目的只是告诉操作系统您的视图控制器可以旋转。如果您想要自定义代码来处理旋转事件,您应该覆盖 -didRotateFromInterfaceOrientation
或其他类似的回调方法之一,具体取决于您的需要:
-
willRotateToInterfaceOrientation:duration:
-
willAnimateRotationToInterfaceOrientation:duration:
-
didRotateFromInterfaceOrientation:
willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
-
didAnimateFirstHalfOfRotationToInterfaceOrientation:
-
willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
See 自动旋转视图 http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/AutorotatingViews/AutorotatingViews.html#//apple_ref/doc/uid/TP40007457-CH11-SW1在开发人员文档中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)