我创建了一个实现popover行为的类。该类创建一个UIWindow
并将视图控制器放在其中。将视图控制器调整为特定尺寸,然后使新窗口可见。只要设备保持相同的方向,一切都很好。我在旋转设备时遇到问题。视图控制器的视图不会根据方向调整大小。代码是:
@implementation Popover
- (void)present {
[self.window addSubview:self.backgroundView];
self.backgroundView.backgroundColor = NICE_BLACK_COLOR;
self.window.rootViewController = self.contentViewController;
[self.window makeKeyAndVisible];
[self animatePresentation];
}
-(void)animatePresentation {
CGRect finalFrame = [self viewControllerFrame];
self.contentViewController.view.frame = [self hiddenFrame];
// trying to set constraints to the view controller's view for when the window rotates. but this isn't working as wanted
self.contentViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
[UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.7 options:0 animations:^{
self.contentViewController.view.frame = finalFrame;
} completion:^(BOOL finished) {
_visible = YES;
}];
}
@end
我考虑的另一个选择是不允许轮换。我尝试的方法是将虚拟视图控制器设置为UIWindow
并禁用其中的旋转。
@implementation NoRotationViewController
-(BOOL)shouldAutorotate {
return NO;
}
@end
并将其设置为UIWindow
。
@implementation Popover
-(void)present {
NoRotationViewController *vc = [NoRotationViewController new];
vc.backgroundColor = [UIColor purpleColor];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
@end
现在弹出窗口不允许旋转但是它下面的主窗口仍然旋转(包括状态栏)
这是打印屏幕。注意状态栏方向和原始窗口的视图控制器(它是一个拆分视图控制器 - 注意主控部分)