简体   繁体   English

隐藏状态栏iOS 7时视图框架发生变化

[英]View frame changes when hiding the status bar iOS 7

I am trying to solve this issue for a while, 我正在尝试解决这个问题一段时间,

I have a view controller embedded in a navigation controller and I want to toggle the status bar hide/show. 我在导航控制器中嵌入了一个视图控制器,我想切换状态栏的隐藏/显示。

The problem is when I set the status bar to be hidden my all view including the navigation bar jumps up. 问题是当我将状态栏设置为隐藏时,包括导航栏在内的所有视图都会跳起来。

How can I avoid this behavior? 如何避免这种行为?
I just want to hide the status bar without any other effects, and with the navigation bar staying extended from 0 to 64 px height. 我只想隐藏状态栏而没有其他任何影响,并且导航栏保持从0到64 px的高度扩展。

I created a simple project that demonstrate the problem. 我创建了一个演示该问题的简单项目

Few notes about possible solution: 关于可能的解决方案的几点注意事项:
- I cant use auto layout -我不能使用自动版式
- the navigation bar cant be translucent -导航栏不能是半透明的
- "View controller-based status bar appearance" must be is set to NO -“基于视图控制器的状态栏外观”必须设置为NO

To be honest, I would recommend that you modify your desires. 老实说,我建议您改变自己的欲望。 The behavior you're describing is built deeply into UINavigationController, so, basically, you can't do what you're describing without writing your own UINavigationController subclass - though, to be even more honest, it is far from clear to me what you'd have to override to interfere with this behavior, so it might be necessary to write a substitute interface. 您描述的行为已深深植入UINavigationController中,因此,基本上,如果不编写自己的UINavigationController子类,您将无法执行所描述的行为-尽管,更坦白地说,我对您所知还不甚了解必须重写以干扰此行为,因此可能有必要编写一个替代接口。 For example, you could have a presented view controller containing a navigation bar and your interface, and now you are in complete control of the height and position of everything... It sounds to me like this is a road that isn't worth going down. 例如,您可能有一个显示的视图控制器,其中包含导航栏和您的界面,现在您可以完全控制所有内容的高度和位置...在我看来,这是一条不值得走的路下。

First change the all your view and button springs and struts to 首先将所有视图更改,并将按钮的弹簧和支柱更改为

在此处输入图片说明

and then change your code to 然后将您的代码更改为

- (IBAction)toggleStatusBar:(UIButton *)sender {
    _hidden = !_hidden;

    [[UIApplication sharedApplication] setStatusBarHidden:_hidden withAnimation:UIStatusBarAnimationSlide];
    [UIView animateWithDuration:0.2 animations:^{
        CGRect frame = self.navigationController.navigationBar.frame ;

        _hidden?(frame.size.height +=20):(frame.size.height -=0);
        self.navigationController.navigationBar.frame = frame;

        CGRect frame2 = self.view.frame ;

        _hidden?(frame2.origin.y +=20):(frame2.origin.y -=0);
        _hidden?(frame2.size.height -=20):(frame.size.height -=0);

        self.view.frame = frame2;

    }];

}

you have to set animation else it will cause problem and show black area when hiding status bar. 您必须设置动画,否则将导致问题并在隐藏状态栏时显示黑色区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM