programing

Swift iOS 앱에서 상태 표시 줄을 숨기려면 어떻게합니까?

new-time 2020. 5. 13. 21:23
반응형

Swift iOS 앱에서 상태 표시 줄을 숨기려면 어떻게합니까?


화면 상단의 상태 표시 줄을 제거하고 싶습니다.작동하지 않습니다.

func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
        application.statusBarHidden = true
        return true
}

나는 또한 시도했다 :

func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    var controller = UIViewController()
    application.statusBarHidden = true
    controller.setNeedsStatusBarAppearanceUpdate()

    var view = UIView(frame: CGRectMake(0, 0, 320, 568))
    view.backgroundColor = UIColor.redColor()
    controller.view = view

    var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
    label.center = CGPointMake(160, 284)
    label.textAlignment = NSTextAlignment.Center
    label.text = "Hello World"
    controller.view.addSubview(label)

    self.window!.rootViewController = controller
    self.window!.makeKeyAndVisible()
    return true
}

뷰 컨트롤러에서 prefersStatusBarHidden을 구현해야합니다.

스위프트 3 이상

override var prefersStatusBarHidden: Bool {
    return true
}

  1. Info.plist 파일로 이동
  2. 해당 라인 중 하나에 마우스를 대면 [+) 및 (-) 버튼이 나타납니다.
  3. 더하기 버튼을 클릭하여 대문자 V로 시작하는 새 키 유형을 추가하면 자동으로 가장 먼저 컨트롤러 기반 상태 표시 줄보기가 표시됩니다.
  4. 이것을 키로 추가하십시오.
  5. VALUE를 "NO"로 설정
  6. 당신에게 AppDelegate.swift로 이동
  7. 메소드 내부에 코드를 추가하십시오.
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
        application.statusBarHidden = true
        return true
    }
    

끝난! 앱을 실행하면 더 이상 상태 표시 줄이 없습니다!


스위프트 3

에서

Info.plist

설정

View controller-based status bar appearance

NO

그리고 전화

UIApplication.shared.isStatusBarHidden = true


당신은 숨길 및 상태 표시 줄을 다시 가져올 경우

버튼을 탭에

있는 동안 제시하고 해산시,

슬라이드 - 인 메뉴

,

팝업

등, 당신은이 방법을 사용할 수 있습니다 : -상태 표시 줄을 숨기려면 :-

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar

상태 표시 줄을 다시 가져 오려면 :-

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal 

코딩보다는 시각적 접근 방식을 선호하는 경우 다음 방법을 사용하십시오.

info.plist

여기에 이미지 설명을 입력하십시오

간단하게 추가

View controller-based status bar appearance

NO

Status bar is initially hidden

같은

YES


 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true);
    navigationController?.navigationBar.hidden = true // for navigation bar hide
    UIApplication.sharedApplication().statusBarHidden=true; // for status bar hide
}

iOS 10 / Swift 3.0 업데이트더 이상 함수, 이제 속성이 아닙니다 ...

override var prefersStatusBarHidden: Bool {
    return true
}

스위프트 3.x에서 :

override func viewWillAppear(_ animated: Bool) {
    UIApplication.shared.isStatusBarHidden = true
}

따라서 여기의 문제는 실제로 Swift와 관련이 없지만 iOS 7에서 상태 표시 줄 모양이 처리되는 방식입니다.기본적으로 뷰 컨트롤러는 화면에있을 때 상태 표시 줄의 모양을 개별적으로 제어합니다. 이 상태 표시 줄을 제어하는이 방법을 사용하려면 모양을 수정하려는 뷰 컨트롤러에 대해 다음 방법을 재정의 할 수 있습니다.

prefersStatusBarHidden

,

preferredStatusBarStyle

,

preferredStatusBarAnimation

,귀하의 경우에는 구현

prefersStatusBarHidden

하고 반환

true

합니다.다른 방법은 응용 프로그램 수준에서 상태 표시 줄 모양을 제어하는 ​​것입니다. 이것은 실제로 설정하려는 것 같습니다 (을 설정하여

application.statusBarHidden

).이 작업을 수행하려면 앱

Info.plist

파일 을 열고 키를 추가하고

UIViewControllerBasedStatusBarAppearance

값을 지정해야

NO

합니다.


Info.plist로 이동하여 두 개의 키를 추가하십시오.

Info.plist로 이동하여 두 개의 키를 추가하십시오.


나는 실제로 이것을 스스로 알아 냈습니다. 솔루션을 다른 옵션으로 추가하겠습니다.

extension UIViewController {
    func prefersStatusBarHidden() -> Bool {
        return true
    }
}

Okay, so this become a problem for me since iOS 9 doesn't support any above the method people have mentioned here such as UIApplication.sharedApplication().statusBarHidden = true or

UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)

and

override func prefersStatusBarHidden() -> Bool {
     return true
}

works but does not provide programable solution where I can change on a condition. (statusBarHidden = true and statusBarHidden = false as we have done before).

Solution to this madness:

By adding to prefersStatusBarHidden() like below you can programmatically control the hide and show of status bar without adding UIViewControllerBasedStatusBarAppearance setting to your info.plist:

var showStatusBar = true

override func prefersStatusBarHidden() -> Bool {
     if showStatusBar {
         return false
     }
     return true
}

private func showStatusBar(enabled: Bool) {
    showStatusBar = enabled
    prefersStatusBarHidden()
}

then use it like this throughout your code:

//Hide Status Bar
showStatusBar(false)

OR

//Show Status Bar
showStatusBar(true)

Just to add, when overriding prefersStatusBarHidden method or variable, the View controller-based status bar appearance in Info.plist must be YES, otherwise the override will have no effect


in Swift 4.2 it is a property now.

override var prefersStatusBarHidden: Bool {
    return true
}

In my case, I was looking for the status bar to hide/show on demand; instead of just when the view loads or disappears.

swift 3.x

//show status bar initially
var showStatusBar = true

//set the parameters
override var prefersStatusBarHidden: Bool {

    if showStatusBar == true {

        //does not prefer status bar hidden
        print("does not prefer status bar hidden")
        return false

    } else {

        //does prefer status bar hidden
        print("does prefer status bar hidden")
    return true

    }
}

//ex: hide status bar and call parameter function again whenever you want
        showStatusBar = false
        setNeedsStatusBarAppearanceUpdate()

A solution that works for me; if you want to hide the status bar on a specific view controller while loading:

import UIKit

class ViewController: UIViewController {

private var hideStatusBar: Bool = false

override var prefersStatusBarHidden: Bool {
    return hideStatusBar
}

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return UIStatusBarAnimation.slide
}

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundcolor = .white
    hideStatusBar = true

    UIView.animate(withDuration: 0.3) {
        self.setNeedsStatusBarAppearanceUpdate()
    }
}

Attention: if you set the key "View controller-based status bar appearance" to "NO" in your info.plist the code above doesn't work. You should set the key to "YES" or remove it from info.plist


In your project General->Deployment Info->Status bar style select check mark of Hide status bar Note:- it hides status bar throughout application


For Swift 4+ try the following code (tested on Swift 4.0, 4.1 - IOS 10, 11) :

override var prefersStatusBarHidden: Bool { return true }

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // call this func to force preferredStatusBarStyle to be read again.
    setNeedsStatusBarAppearanceUpdate()
}

Swift 5: In the main view controller, or main navigation controller if you have,

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override var prefersStatusBarHidden: Bool {
        return false
    }

And "View controller-based status bar appearance" in plist must be YES, otherwise the above code will not be called.

If you want to hide status bar when launching app, "Status bar is initially hidden" in plist must be YES. This can prevent launch image from being distorted when extra blue bar showing on screen top.


I'm using Xcode 8.1 (8B62) with a deployment target set to 10.1 and I haven't had much luck with the override options mentioned above. However checking the "Hide status bar" option in Deployment Info did the trick for me.

Project > General

I hope this helps.


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        application.isStatusBarHidden = true
        return true
    }

You can use this code in your ViewController Class scope

open override var prefersStatusBarHidden: Bool { return true }

In your Project->General->Deployment info

Statusbar Style:--

just marked Hide status Bar(iOS 10)


Swift 4

//MARK:- Show Status Bar
UIApplication.shared.isStatusBarHidden = false

//MARK:- Hide Status Bar
UIApplication.shared.isStatusBarHidden = true

뷰 컨트롤러를 모달로 제시하는 경우 시도해보십시오

viewController.hidesBottomBarWhenPushed = true
viewController.modalPresentationCapturesStatusBarAppearance = true

참고 URL :

https://stackoverflow.com/questions/24236912/how-do-i-hide-the-status-bar-in-a-swift-ios-app

반응형