UIStatusBar 不会消失
UIStatusBar won't disappear
我试图在 Swift 中创建一个 class,它会在 1 秒后自动隐藏我的 UIStatusBar
和 navigationController
。
我的问题是 StatusBar
不会消失。这是我得到的:
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "prefersStatusBarHidden", userInfo: nil, repeats: false)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Fade
}
override func prefersStatusBarHidden() -> Bool {
if (barcounter == 0){
hide()
barcounter = 1
return true
}
else {
show()
barcounter = 0
return false
}
}
@IBAction func picturePressed(sender: AnyObject) {
prefersStatusBarHidden()
}
func hide(){
UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navigationController?.navigationBar.alpha = 0.0
}, completion: nil)
}
func show(){
UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navigationController?.navigationBar.alpha = 1.0
}, completion: nil)
}
您需要在您想隐藏 uistatusbar 的任何视图控制器中覆盖此方法。
override func prefersStatusBarHidden() -> Bool {
return true;
}
如果它不起作用,那么试试这个:-
In Info.plist set View controller-based status bar appearance to NO
And call UIApplication.sharedApplication().statusBarHidden = true
希望对您有所帮助。
好吧..我是这样解决的:
我创建了一个新的 class HeaderAnimationHelper
,我在其中创建了可用的方法。这样我就可以从任何地方调用它了。
所以在这里你可以看到助手class:
导入 UIKit
class HeaderAnimationHelper {
static let sharedInstance = HeaderAnimationHelper()
var navi: UINavigationController!
func hideController(var barcounter: Int, navigationController: UINavigationController) -> Int {
navi = navigationController
if (barcounter == 0){
barcounter = 1
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
hide()
}
else {
show()
barcounter = 0
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
}
return barcounter
}
func hide(){
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navi.navigationBar.alpha = 0.0
}, completion: nil)
}
func show(){
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navi.navigationBar.alpha = 1.0
}, completion: nil)
}
}
下一个 class 是主要的 class,您可以在其中放置所有代码和东西...
我是这样创建的:
import UIKit
class ContactMeViewController: UIViewController {
var barcounter = 0
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "animate", userInfo: nil, repeats: false)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Fade
}
@IBAction func picturePressed(sender: AnyObject) {
animate()
}
func animate(){
barcounter = HeaderAnimationHelper.sharedInstance.hideController(barcounter, navigationController: self.navigationController!)
}
}
编辑 2015 年 10 月 7 日:
我忘了说,将依赖项添加到 Info.plist
很重要
In Info.plist set View controller-based status bar appearance to NO
注意这个方法UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
已被贬低
我试图在 Swift 中创建一个 class,它会在 1 秒后自动隐藏我的 UIStatusBar
和 navigationController
。
我的问题是 StatusBar
不会消失。这是我得到的:
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "prefersStatusBarHidden", userInfo: nil, repeats: false)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Fade
}
override func prefersStatusBarHidden() -> Bool {
if (barcounter == 0){
hide()
barcounter = 1
return true
}
else {
show()
barcounter = 0
return false
}
}
@IBAction func picturePressed(sender: AnyObject) {
prefersStatusBarHidden()
}
func hide(){
UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navigationController?.navigationBar.alpha = 0.0
}, completion: nil)
}
func show(){
UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navigationController?.navigationBar.alpha = 1.0
}, completion: nil)
}
您需要在您想隐藏 uistatusbar 的任何视图控制器中覆盖此方法。
override func prefersStatusBarHidden() -> Bool {
return true;
}
如果它不起作用,那么试试这个:-
In Info.plist set View controller-based status bar appearance to NO
And call UIApplication.sharedApplication().statusBarHidden = true
希望对您有所帮助。
好吧..我是这样解决的:
我创建了一个新的 class HeaderAnimationHelper
,我在其中创建了可用的方法。这样我就可以从任何地方调用它了。
所以在这里你可以看到助手class:
导入 UIKit
class HeaderAnimationHelper {
static let sharedInstance = HeaderAnimationHelper()
var navi: UINavigationController!
func hideController(var barcounter: Int, navigationController: UINavigationController) -> Int {
navi = navigationController
if (barcounter == 0){
barcounter = 1
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
hide()
}
else {
show()
barcounter = 0
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
}
return barcounter
}
func hide(){
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navi.navigationBar.alpha = 0.0
}, completion: nil)
}
func show(){
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navi.navigationBar.alpha = 1.0
}, completion: nil)
}
}
下一个 class 是主要的 class,您可以在其中放置所有代码和东西... 我是这样创建的:
import UIKit
class ContactMeViewController: UIViewController {
var barcounter = 0
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "animate", userInfo: nil, repeats: false)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Fade
}
@IBAction func picturePressed(sender: AnyObject) {
animate()
}
func animate(){
barcounter = HeaderAnimationHelper.sharedInstance.hideController(barcounter, navigationController: self.navigationController!)
}
}
编辑 2015 年 10 月 7 日:
我忘了说,将依赖项添加到 Info.plist
很重要In Info.plist set View controller-based status bar appearance to NO
注意这个方法UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
已被贬低