Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS 9中不推荐使用setStatusBarHidden(:withAnimation:)_Ios_Swift_Ios9_Statusbar - Fatal编程技术网

在iOS 9中不推荐使用setStatusBarHidden(:withAnimation:)

在iOS 9中不推荐使用setStatusBarHidden(:withAnimation:),ios,swift,ios9,statusbar,Ios,Swift,Ios9,Statusbar,我看到,在iOS 9中,setStatusBarHidden(:withAnimation:)现在已被弃用,文档中说要使用[UIViewController prefersStatusBarHidden],但如果我仍然想用幻灯片动画隐藏状态栏,在iOS 9中还有什么替代方法呢?请参阅preferredStatusBarUpdateAnimation, Gif 代码 Swift 3 计算变量已经取代了一些函数 动画功能已更新语法 通过将更新移动到didSet(swift3语法),我已经清理

我看到,在iOS 9中,setStatusBarHidden(:withAnimation:)现在已被弃用,文档中说要使用
[UIViewController prefersStatusBarHidden]
,但如果我仍然想用幻灯片动画隐藏状态栏,在iOS 9中还有什么替代方法呢?

请参阅
preferredStatusBarUpdateAnimation

Gif

代码


Swift 3

  • 计算变量已经取代了一些函数
  • 动画功能已更新语法


通过将更新移动到
didSet
swift3语法),我已经清理了Leo的惊人答案


如果你是用objective c编写代码,下面是解决方案:)(Leo的objective c版本:p谢谢你!!!)

声明一个变量

bool isHidden;
isHidden = false;//in viewDidload()
然后在要隐藏状态栏时添加此代码

isHidden = true;
[UIView animateWithDuration:0.6 animations:^{
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}];
然后添加这两种方法

-(UIStatusBarAnimation) preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationFade;
}

-(BOOL) prefersStatusBarHidden
{ return isHidden;}
希望你的问题能得到解决(微笑)

  • SWIFT 3备选方案
嘿,伙计们,在Swift 3中找到了一种更简洁的方法,通过对每个覆盖使用私有var配对。 我原来的帖子:

但这里是它的jist:

下面是一个片段:

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    get {
        return .slide
    }
}

private var statusBarStyle : UIStatusBarStyle = .default

override var preferredStatusBarStyle: UIStatusBarStyle {
    get {
        return statusBarStyle
    }
}

private var statusBarStatus : Bool = false

override var prefersStatusBarHidden: Bool {
    get {
        return statusBarStatus
    }
}
然后我可以调用这样的函数:(这是我的一个例子,所以忽略自定义函数)


我的即使在目标块中也没有动画。有什么想法吗?它与“基于视图控制器的状态栏外观”=否一起工作吗?还没有测试,但我猜在设置“基于视图控制器的状态栏外观”=是后,当此键为Nomine时不工作。非常感谢。如果您需要查看基于控制器的状态栏外观,这可能会有所帮助:我希望我会记得不时使用didSet。。。我只是习惯了我以前的编码方式,但现在好多了:)这是一个很好的解决方案。我没有想过让状态隐藏标志动态化。
isHidden = true;
[UIView animateWithDuration:0.6 animations:^{
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}];
-(UIStatusBarAnimation) preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationFade;
}

-(BOOL) prefersStatusBarHidden
{ return isHidden;}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    get {
        return .slide
    }
}

private var statusBarStyle : UIStatusBarStyle = .default

override var preferredStatusBarStyle: UIStatusBarStyle {
    get {
        return statusBarStyle
    }
}

private var statusBarStatus : Bool = false

override var prefersStatusBarHidden: Bool {
    get {
        return statusBarStatus
    }
}
func sliderView(sliderView: SliderView, didSlideToPlace: CGFloat, within: CGFloat) {

    let val = (within - (didSlideToPlace - sliderView.upCent))/(within)
    print(val)
    //Where you would change the private variable for the color, for example.
    if val > 0.5 {
        statusBarStyle = .lightContent
    } else {
        statusBarStyle = .default
    }
    UIView.animate(withDuration: 0.5, animations: {
        sliderView.top.backgroundColor = UIColor.black.withAlphaComponent(val)
        self.coverLayer.alpha = val
        self.scroll.backgroundColor = colors.lightBlueMainColor.withAlphaComponent(val)
    }, completion: {
        value in
        //If you do not call setNeedsStatusBarAppearanceUpdate() in an animation block, the animation variable won't be called it seems.
        UIView.animate(withDuration: 0.4, animations: {

            self.animating = true

            //Where you set the status for the bar (your part of the solution)
            self.statusBarStatus = false

            //Then you call for the refresh
            self.setNeedsStatusBarAppearanceUpdate()
        })
    })
}