Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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中设置状态栏以显示消息?_Ios_Swift - Fatal编程技术网

如何在iOS中设置状态栏以显示消息?

如何在iOS中设置状态栏以显示消息?,ios,swift,Ios,Swift,我希望在状态栏下方设置一条消息,并可能更改消息的颜色(请参阅照片-我希望在“睡眠周期”所在的位置设置一条消息) 然而,我看过iOS的人性化设计指南,但无法确定该控件的名称 只要在正确的方向上指出一点就会有很大的帮助。试试下面的代码:在Xcode 8中测试的代码 /用下面的代码更新你的plist View controller-based status bar appearance = NO //在你的VC中: override func viewDidLoad() {

我希望在状态栏下方设置一条消息,并可能更改消息的颜色(请参阅照片-我希望在“睡眠周期”所在的位置设置一条消息)

然而,我看过iOS的人性化设计指南,但无法确定该控件的名称


只要在正确的方向上指出一点就会有很大的帮助。

试试下面的代码:在Xcode 8中测试的代码

/用下面的代码更新你的plist

 View controller-based status bar appearance = NO  
//在你的VC中:

     override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    title = "Some Title"

    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.barTintColor = UIColor.red
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    UIApplication.shared.statusBarStyle = .lightContent  // To change your status bar to display light content (In white colour)
   }

    func sleepCycleNotify() {

    // To set BannerView
    let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height)))
    barView.backgroundColor=UIColor.red // set to any colour you want..
    navigationController?.navigationBar.addSubview(barView)

    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)


    // Animation 1:
    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!


        }, completion:{ finished in



            UIView.animate(withDuration: 1, delay: 1.5, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{

                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)

                }, completion: nil)

    })


}
上述代码的输出:

//动画2:

    func sleepCycleNotify() {

    // 
    let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height)))
    barView.backgroundColor=UIColor.red // set any colour you want..
    navigationController?.navigationBar.addSubview(barView)

    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)



    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{

        UIApplication.shared.isStatusBarHidden = true
        UINavigationController().navigationBar.isHidden = true
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!




        }, completion:{ finished in



            UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{
               // notifyLabel.alpha = 0...1
                UIApplication.shared.isStatusBarHidden = false
                UINavigationController().navigationBar.isHidden = false
                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)


                }, completion: nil)

    })


}
动画2的输出:

    func sleepCycleNotify() {

    // 
    let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height)))
    barView.backgroundColor=UIColor.red // set any colour you want..
    navigationController?.navigationBar.addSubview(barView)

    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)



    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{

        UIApplication.shared.isStatusBarHidden = true
        UINavigationController().navigationBar.isHidden = true
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!




        }, completion:{ finished in



            UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{
               // notifyLabel.alpha = 0...1
                UIApplication.shared.isStatusBarHidden = false
                UINavigationController().navigationBar.isHidden = false
                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)


                }, completion: nil)

    })


}

改进的答案:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    title = "Some Title"

    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    navigationController?.navigationBar.barTintColor = UIColor.purple
    UIApplication.shared.statusBarStyle = .lightContent

  }

    func sleepCycleNotify() {

    // To set BannerView
    let barView = UIView(frame: CGRect(x:0, y:-UIApplication.shared.statusBarFrame.height, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height) + UIApplication.shared.statusBarFrame.height ))
    barView.backgroundColor=UIColor.red // set to any colour you want..
    navigationController?.navigationBar.addSubview(barView)


    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:UIApplication.shared.statusBarFrame.height, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.numberOfLines = 0
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)


    // Animation 1:
    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!



        }, completion:{ finished in



            UIView.animate(withDuration: 1, delay: 1.5, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{

                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)

                }, completion: nil)

    })   
}
动画3的输出:


试试这段代码:在Xcode 8中测试的代码

/用下面的代码更新你的plist

 View controller-based status bar appearance = NO  
//在你的VC中:

     override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    title = "Some Title"

    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.barTintColor = UIColor.red
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    UIApplication.shared.statusBarStyle = .lightContent  // To change your status bar to display light content (In white colour)
   }

    func sleepCycleNotify() {

    // To set BannerView
    let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height)))
    barView.backgroundColor=UIColor.red // set to any colour you want..
    navigationController?.navigationBar.addSubview(barView)

    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)


    // Animation 1:
    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!


        }, completion:{ finished in



            UIView.animate(withDuration: 1, delay: 1.5, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{

                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)

                }, completion: nil)

    })


}
上述代码的输出:

//动画2:

    func sleepCycleNotify() {

    // 
    let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height)))
    barView.backgroundColor=UIColor.red // set any colour you want..
    navigationController?.navigationBar.addSubview(barView)

    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)



    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{

        UIApplication.shared.isStatusBarHidden = true
        UINavigationController().navigationBar.isHidden = true
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!




        }, completion:{ finished in



            UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{
               // notifyLabel.alpha = 0...1
                UIApplication.shared.isStatusBarHidden = false
                UINavigationController().navigationBar.isHidden = false
                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)


                }, completion: nil)

    })


}
动画2的输出:

    func sleepCycleNotify() {

    // 
    let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height)))
    barView.backgroundColor=UIColor.red // set any colour you want..
    navigationController?.navigationBar.addSubview(barView)

    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:0, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)



    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{

        UIApplication.shared.isStatusBarHidden = true
        UINavigationController().navigationBar.isHidden = true
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!




        }, completion:{ finished in



            UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{
               // notifyLabel.alpha = 0...1
                UIApplication.shared.isStatusBarHidden = false
                UINavigationController().navigationBar.isHidden = false
                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)


                }, completion: nil)

    })


}

改进的答案:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    title = "Some Title"

    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    navigationController?.navigationBar.barTintColor = UIColor.purple
    UIApplication.shared.statusBarStyle = .lightContent

  }

    func sleepCycleNotify() {

    // To set BannerView
    let barView = UIView(frame: CGRect(x:0, y:-UIApplication.shared.statusBarFrame.height, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height) + UIApplication.shared.statusBarFrame.height ))
    barView.backgroundColor=UIColor.red // set to any colour you want..
    navigationController?.navigationBar.addSubview(barView)


    let notifyLabel = UILabel()
    notifyLabel.frame = CGRect(x:0, y:UIApplication.shared.statusBarFrame.height, width:view.frame.width, height:(UINavigationController().navigationBar.frame.height))
    notifyLabel.backgroundColor=UIColor.clear
    notifyLabel.numberOfLines = 0
    notifyLabel.text = "Sleep Cycle"
    notifyLabel.textAlignment = .center
    notifyLabel.textColor = UIColor.white
    notifyLabel.alpha = 0.8
    barView.addSubview(notifyLabel)


    // Animation 1:
    // To achive animation
    barView.center.y -= (navigationController?.navigationBar.bounds.height)!


    UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.6, options: UIViewAnimationOptions.curveEaseIn, animations:{
        barView.center.y += (self.navigationController?.navigationBar.frame.height)!



        }, completion:{ finished in



            UIView.animate(withDuration: 1, delay: 1.5, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseOut, animations:{

                barView.center.y -= ((self.navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)

                }, completion: nil)

    })   
}
动画3的输出:


是否类似于?否:)此“状态栏消息”存在于应用程序之外。这似乎是应用程序内部的。除非您使用一些后台服务,如录音,否则您无法从应用程序外部执行此操作。这个系统也表明,你无法控制it@sandeep我上面展示的示例是设置我的应用程序睡眠周期。该条存在于应用程序之外。是的,它会在背景中跟踪你的动作。@joe感谢joe给出的详细答案。我认为这在应用程序之外不起作用。它类似于吗?不:)这个“状态栏消息”存在于应用程序之外。这似乎是应用程序内部的。除非您使用一些后台服务,如录音,否则您无法从应用程序外部执行此操作。这个系统也表明,你无法控制it@sandeep我上面展示的示例是设置我的应用程序睡眠周期。该条存在于应用程序之外。是的,它会在背景中跟踪你的动作。@joe感谢joe给出的详细答案。我认为这在应用程序之外是行不通的。