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

标签 ios swift

enter image description here

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

但是,我查看了 iOS 的人性化设计指南,但无法确定该控件的名称。

https://developer.apple.com/ios/human-interface-guidelines/ui-bars/navigation-bars/

只要指向正确的方向就会有很大的帮助。

最佳答案

试试这段代码:在 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)

    })


}

以上代码的输出:

enter image description here

//动画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 的输出:

enter image description here

改进答案:

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 的输出:

enter image description here

关于ios - 如何在 iOS 中设置状态栏以显示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40124169/

相关文章:

ios - 如何将添加文本框合并到 navigationItem -> rightBarButtonItem?

ios - AdMob Ios 导致帧率下降

swift - Swift 中优雅的 `bounded` 方法

ios - swift4:无法将值从当前弹出窗口传递到上一个 View Controller

IOS 如果模式被取消,如何在 ViewController 中调用方法

ios - 有什么方法可以连续为 Collection View 单元格调用 initialLayoutAttributesForAppearingItemAtIndexPath 并延迟一些?

ios - 延迟 5 秒后调用 Admob 插页式 - Swift iOS

ios - 为什么 Auth.auth().currentUser?.uid 返回 nil 值?

ios - 纯代码关于UICollectionView水平分页

objective-c - 我可以更改 Objective C 中文本字段的底部边框线颜色吗?