how to hide status bar and navigation bar when tap device
With Swift 5 and iOS 12, according to you needs, you may select one of the three following code snippets in order to solve your problem.
#1. Using UINavigationController
hidesBarsOnTap
property + UIViewController
prefersStatusBarHidden
and preferredStatusBarUpdateAnimation
properties
Since iOS 8, UINavigationController
has a hidesBarsOnTap
property. hidesBarsOnTap
has the following declaration:
var hidesBarsOnTap: Bool { get set }
A Boolean value indicating whether the navigation controller allows hiding of its bars using a tap gesture.
Apple also states about hidesBarsOnTap
:
When the value of this property is
true
, the navigation controller toggles the hiding and showing of its navigation bar and toolbar in response to an otherwise unhandled tap in the content area. The default value of this property isfalse
.
The following code shows how to implement hidesBarsOnTap
:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.hidesBarsOnTap = true
}
override var prefersStatusBarHidden: Bool {
return navigationController?.isNavigationBarHidden == true
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return UIStatusBarAnimation.slide
}
}
#2. Using UINavigationController
setNavigationBarHidden(_:animated:)
method + UIViewController
prefersStatusBarHidden
and preferredStatusBarUpdateAnimation
properties with a UIButton
UINavigationController
has a method called setNavigationBarHidden(_:animated:)
. setNavigationBarHidden(_:animated:)
has the following declaration:
func setNavigationBarHidden(_ hidden: Bool, animated: Bool)
Sets whether the navigation bar is hidden.
The following code shows how to toggle your status bar and navigation bar by using setNavigationBarHidden(_:animated:)
with a UIButton
set in the Storyboard and linked to a @IBAction
:
import UIKit
class ViewController: UIViewController {
// Link this @IBAction to a `UIButton`
@IBAction func toggle(_ sender: UIButton) {
navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)
}
override var prefersStatusBarHidden: Bool {
return navigationController?.isNavigationBarHidden == true
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return UIStatusBarAnimation.slide
}
}
#3. Using UINavigationController
setNavigationBarHidden(_:animated:)
method + UIViewController
prefersStatusBarHidden
and preferredStatusBarUpdateAnimation
properties with a UIGestureRecognizer
As an alternative to the previous code, you can use setNavigationBarHidden(_:animated:)
with a UIGestureRecognizer
instead of a UIButton
:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let gesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.toggle))
view.isUserInteractionEnabled = true
view.addGestureRecognizer(gesture)
}
@objc func toggle() {
navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)
}
override var prefersStatusBarHidden: Bool {
return navigationController?.isNavigationBarHidden == true
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return UIStatusBarAnimation.slide
}
}
- Make sure that
UIViewControllerBasedStatusBarAppearance
is set totrue
in your project's Info.plist, otherwise the previous sample codes won't work. - See this answer for a similar question if you need to target iOS 10.
How to hide Navigation and Status Bars when tapped - with animation
Try this
var statusBarHidden = false
func tapAction() {
self.navigationController?.navigationBarHidden = true
self.statusBarHidden = true
self.setNeedsStatusBarAppearanceUpdate()
}
override func prefersStatusBarHidden() -> Bool {
return statusBarHidden
}
Hide status bar whenever nav bar is hidden - SWIFT iOS8
Sorry if this answer is a little late, but here is one way to do it.
Use the prefersStatusBarHidden() method within your view controller.
override func prefersStatusBarHidden() -> Bool {
if self.navigationController?.navigationBarHidden == true {
return true
} else {
return false
}
}
Basically says that when the Nav bar is hidden, then the status bar is too and vice versa.
Hide/Unhide StatusBar, TabBar and NavigationBar on touch
Adding a tap gesture to the view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideUIComponents:)];
[self.view addGestureRecognizer:tapGestureRecognizer];
Then the function hideUIComponents
- (void)hideUIComponents:(UITapGestureRecognizer*)tapGesture
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionMoveIn];
[[self.view.window layer] addAnimation:animation forKey:@"layerAnimation"];
[self.tabBarController.tabBar setHidden:YES];
}
Unhide by reversing the values. I hope this helps.
How remove navigation bar and status bar?
You just have to add this :
android:name=".Account.SplashActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
</activity>
Simple!
Android - hide status and navigation bar completely for an app with nav drawer and app bar
Update
The issue was with your layout file. I just set android:fitsSystemWindows=false
to fix the issue. I made a pull request to your repo, which I think solves your issue.
You should follow the following official documentations:
- Hide the status bar
- Hide the navigation bar
Hide the Status Bar on Android 4.0 and Lower
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}
...
}
Hide the Status Bar on Android 4.1 and Higher
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
Hide the Navigation Bar
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
Related Topics
Unable to Save On/Off State of a Uitableviewcell
How to Read a Property List from Data in Swift 3
Suppressing Implicit Returns in Swift
Swift 2.0 Get Mirrored Superclass Properties
Load Desktop Version Wkwebview iOS 9
Naming Convention for Optional Binding
Wkwebview Auto Fill Login Form Swift 2
Converting Swift 2.3 to Swift 3.0 - Error, Cannot Invoke 'Datatask' with an Argument List of Type'
Swift: How to Get Form Values Using Eureka Form Builder
How to Encode an Unmanaged<Seckey> to Base64 to Send to Another Server
Swift (iOS 8 Sdk) Convert Unmanaged<Abmultivalueref> to Abmultivalueref