我有一个可以包含两个子视图控制器的自定义视图控制器。当设备处于纵向时,可以看到其中一个控制器的视图。当设备处于横向时,另一个控制器的视图可见。但是,当横向视图可见时,状态栏会缩回,以便为特定视图腾出更多空间。设备转回纵向模式后,状态栏将取消隐藏。这是自定义视图控制器显示在UINavigationController
.
我的问题是当状态栏的可见性发生变化时,我的子视图没有正确调整。当您以不同的方向转动设备时,最终会出现很大的间隙和/或重叠,如下图所示:
如您所见,它最初很好(纵向),但是当设备转动时,状态栏所在的位置有一个白色间隙。当设备转回纵向时,UINavigationController
会弹出导航栏并与状态栏重叠,并且导航栏与其下方的视图之间会出现间隙。如果您非常快速地从一个横向旋转 180 度到相反的横向,间隙就会消失并且看起来不错。
下面的方法属于自定义视图控制器,被调用willAnimateRotationToInterfaceOrientation:duration:
(显然是为了处理旋转事件)和viewDidAppear:
(当视图从导航堆栈中的前一个视图控制器推入时进行处理)。
- (void)cueAnimationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation fromViewDidAppear:(BOOL) fromViewDidAppear
{
// Fading animation during orientation flip.
// Make sure its not already animating before trying.
BOOL barHidden = [UIApplication sharedApplication].statusBarHidden;
if (!isAnimating) {
BOOL alreadyGoodGrid = (UIInterfaceOrientationIsLandscape(interfaceOrientation) && curView == self.gridViewController.view);
BOOL alreadyGoodTable = (UIInterfaceOrientationIsPortrait(interfaceOrientation) && curView == self.tableViewController.view);
if ((alreadyGoodGrid && barHidden) ||
(alreadyGoodTable && !barHidden)) {
// If views are the way they should be for this orientation. Don't do
// anything.
return;
}
isAnimating = YES;
UIView *nextView;
// Get starting orientation. This will determine what view goes on top
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
nextView = self.gridViewController.view;
else
nextView = self.tableViewController.view;
if (nextView == self.tableViewController.view)
{
if (!alreadyGoodTable)
{
self.tableViewController.view.alpha = 0.0;
[self.view bringSubviewToFront:self.tableViewController.view];
}
// Unhide the bar for the table view
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
else // gridViewController
{
if (!alreadyGoodGrid)
{
self.gridViewController.view.alpha = 0.0;
[self.view bringSubviewToFront:self.gridViewController.view];
}
// Hide the bar for the grid view
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
[UIView animateWithDuration:0.4
delay: 0.0
options: UIViewAnimationOptionAllowUserInteraction
animations:^{
if (nextView == self.tableViewController.view) {
self.tableViewController.view.alpha = 1.0;
}
else {
self.gridViewController.view.alpha = 1.0;
}
}
completion:^(BOOL finished) {
if (nextView == self.tableViewController.view) {
curView = self.tableViewController.view;
}
else {
curView = self.gridViewController.view;
}
isAnimating = NO;
}];
}
}
非常感谢任何可以花时间看这个的人。