Reputation: 127
I've tried to create a class in Swift, which autohides my UIStatusBar
and my navigationController
after 1 Second.
My problem is, that the StatusBar
is not going to disappear. This is what I got:
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)
}
Upvotes: 0
Views: 189
Reputation: 127
Alright.. I solved it like that:
I created a new class HeaderAnimationHelper
in which I created the useable methods. Like that I can call it from everywhere.
So here you can see the Helper class:
import 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)
}
}
and the next class is the main class in which you can put all you code and stuff... I created it like that:
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!)
}
}
edit 10/07/15:
I've forgotten to mention, that it's important to add the dependency to the Info.plist
In Info.plist set View controller-based status bar appearance to NO
Watch out this method UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
is depricated
Upvotes: 0
Reputation: 557
You need to override this method in whichever view controller u want to hide uistatusbar.
override func prefersStatusBarHidden() -> Bool {
return true;
}
if its not work then try this:-
In Info.plist set View controller-based status bar appearance to NO
And call UIApplication.sharedApplication().statusBarHidden = true
hope this helps you.
Upvotes: 1