gpt4 book ai didi

iOS 隐藏和取消隐藏状态栏不正确移动 subview

转载 作者:可可西里 更新时间:2023-11-01 05:56:21 24 4
gpt4 key购买 nike

我有一个可以包含两个 subview Controller 的自定义 View Controller 。当设备处于纵向时,其中一个 Controller 的 View 可见。当设备处于横向时,另一个 Controller 的 View 可见。但是,当横向 View 可见时,状态栏会缩回以为特定 View 腾出更多空间。设备返回纵向模式后,状态栏将取消隐藏。这是显示在 UINavigationController 中的自定义 View Controller 。

我的问题是当状态栏的可见性改变时我的 subview 没有正确调整。当您以不同的方向转动设备时,最终会出现很大的间隙和/或重叠,如下图所示: enter image description here

如您所见,它最初很好(纵向),但当设备转动时,状态栏所在的位置出现白色间隙。当设备转回纵向时,UINavigationController 的导航栏被调出并与状态栏重叠,导航栏与其下方的 View 之间出现间隙。如果您非常快速地从一个横向方向旋转 180 度到相反的横向方向,间隙就会消失并且看起来不错。

下面的方法属于自定义 View Controller ,在willAnimateRotationToInterfaceOrientation:duration:(显然是处理旋转事件)和viewDidAppear:(处理 View 何时从导航堆栈中的前一个 View Controller 插入)。

- (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;
}];
}
}

非常感谢任何花时间看这个的人。

最佳答案

很多人似乎都遇到过这个问题,我也遇到过,还有其他关于它的 Q/A 线程,您应该搜索一下——我可能永远找不到神奇的答案。您可能会尝试在某些情况下似乎“解决”问题的一种方法是,当您将栏可见性更改为(重新)显示状态栏时:

  • 隐藏导航栏。
  • 取消隐藏状态栏。
  • 取消隐藏导航栏。

如果您要隐藏状态栏,则可以隐藏、隐藏、取消隐藏。

有时人们发现在取消隐藏导航栏之前需要稍微延迟——因此在异步调度 block 中执行此操作以在下一个运行循环中运行。像这样的东西:

dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController setNavigationBarHidden:NO animated:NO];
});

关于iOS 隐藏和取消隐藏状态栏不正确移动 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15191085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com