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
}
prefer Status Bar를 구현해야 합니다.뷰 컨트롤러에 숨김:
스위프트 3 이후
override var prefersStatusBarHidden: Bool {
return true
}
- Info.plist 파일로 이동합니다.
- 이러한 행 중 하나를 가리키면 (+) 및 (-) 버튼이 표시됩니다.
- 더하기 버튼을 클릭하여 대문자 V로 시작하는 새 키 유형을 추가합니다. 첫 번째 선택 항목은 자동으로 View 컨트롤러 기반 상태 표시줄이 표시됩니다.
- 그것을 KEY로 추가합니다.
- 값을 "NO"로 설정합니다.
- AppDelegate.swift로 이동
메서드 내부에 코드를 추가합니다.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool { application.statusBarHidden = true return true }
완료! 앱을 실행하고 더 이상 상태 표시줄을 사용하지 마십시오!
스위프트 3
»Info.plist
설정하다View controller-based status bar appearance
로로 합니다.NO
call화 and화 。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
as ~하듯이YES
iOS 10 / Swift 3.0 업데이트
더 이상 기능이 아니라, 이제는 재산...
override var prefersStatusBarHidden: Bool {
return true
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true);
navigationController?.navigationBar.hidden = true // for navigation bar hide
UIApplication.sharedApplication().statusBarHidden=true; // for status bar hide
}
Info.plist로 이동하여 다음 두 개의 키를 추가합니다.
Swift 3.x:
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.isStatusBarHidden = true
}
따라서 이 문제는 실제로 Swift와는 무관하며 iOS 7에서 상태 표시줄이 어떻게 처리되는지와 관련이 있습니다.
기본적으로 뷰 컨트롤러는 화면에 상태 표시줄의 모양을 개별적으로 제어합니다.상태 표시줄을 제어하는 이 방법을 사용하려면 모양을 수정하려는 보기 컨트롤러에 대해 다음 방법을 재정의할 수 있습니다.
prefersStatusBarHidden
,preferredStatusBarStyle
,preferredStatusBarAnimation
,
In your case, you would just implement 고객님의 경우,prefersStatusBarHidden
and return 및 반환true
.
다른 방법은 애플리케이션 수준에서 상태 표시줄을 제어할 수 있습니다.다른 방법은 응용 프로그램 수준에서 상태 표시줄 모양을 제어하는 것입니다.이것은 당신이 실제로 할 수 있는 것 같습니다. 제 행 고 는 같 습 것 정설 this seems'이 trying (( setting있 do to to you what하re다것니, actuallybyapplication.statusBarHidden
를 참조해 주세요.
이 일을 하기 위해서는 앱 업 행 을 의 하 in려 order work, up'앱 this to app makes to need수면 your you open작이, work this 이 일을 해야 한다.Info.plist
파일 및 키 링 가 추 file 키 and the add key및파일 file )UIViewControllerBasedStatusBarAppearance
it of of , 、 、 of 、 of 。NO
.
스위프트 5 이상
내 경우 일부 조건에 따라 숨겨진 상태 표시줄을 업데이트해야 합니다.
에 베이스 .BaseViewController
성질을 있습니다.hideStatusBar
.
다른 뷰 컨트롤러는 이 기본 컨트롤러의 하위 클래스입니다.으로 상태 동작을 이 상태 표시줄 동작을 .hideStatusBar
가치.
class BaseViewController: UIViewController {
var hideStatusBar: Bool = false {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var prefersStatusBarHidden: Bool {
return hideStatusBar
}
}
사용방법
final class ViewController: BaseViewController, UIScrollViewDelegate {
let scrollView = UIScrollView()
...
func scrollViewDidScroll(_ scrollView: UIScrollView) {
UIView.animate(withDuration: 0.3) {
if scrollView.contentOffset.y > 100 {
self.hideStatusBar = true
} else {
self.hideStatusBar = false
}
}
}
}
데모
여기 데모가 있습니다.UIView.animate(...)
보다 부드럽게 이행할 수 있습니다.
사실 내가 직접 알아냈죠다른 옵션으로 솔루션을 추가합니다.
extension UIViewController {
func prefersStatusBarHidden() -> Bool {
return true
}
}
네, iOS 9는 여기서 언급한 방법 이상의 기능을 지원하지 않기 때문에 문제가 됩니다.UIApplication.sharedApplication().statusBarHidden = true
또는
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)
그리고.
override func prefersStatusBarHidden() -> Bool {
return true
}
는 동작하지만 조건에 따라 변경할 수 있는 프로그램 가능한 솔루션을 제공하지 않습니다.(statusBarHidden = true
그리고.statusBarHidden = false
지금까지와 같이).
이 광기에 대한 해결책:
에 추가함으로써prefersStatusBarHidden()
아래와 같이 추가 없이 상태 표시줄의 숨김 및 표시를 프로그래밍 방식으로 제어할 수 있습니다.UIViewControllerBasedStatusBarAppearance
사용자 정보에 대한 설정:
var showStatusBar = true
override func prefersStatusBarHidden() -> Bool {
if showStatusBar {
return false
}
return true
}
private func showStatusBar(enabled: Bool) {
showStatusBar = enabled
prefersStatusBarHidden()
}
코드 전체에서 다음과 같이 사용합니다.
//Hide Status Bar
showStatusBar(false)
또는
//Show Status Bar
showStatusBar(true)
덧붙이자면 덮어쓸 때prefersStatusBarHidden
메서드 또는 변수,View controller-based status bar appearance
Info.plist는 YES여야 합니다.그렇지 않으면 덮어쓰기가 적용되지 않습니다.
Swift 4.2에서는 현재 자산입니다.
override var prefersStatusBarHidden: Bool {
return true
}
내 경우 뷰가 로드되거나 사라졌을 때 대신 필요에 따라 표시/숨기기 위한 상태 표시줄을 찾고 있었습니다.
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()
Swift 4+의 경우 다음 코드를 시험합니다(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: 메인 뷰 컨트롤러 또는 메인 내비게이션 컨트롤러(있는 경우)에서
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override var prefersStatusBarHidden: Bool {
return false
}
또한 plist의 "View controller-based status bar apaurance"는 YES여야 합니다.그렇지 않으면 위의 코드가 호출되지 않습니다.
앱을 실행할 때 상태 표시줄을 숨기려면 목록 내의 "상태 표시줄이 처음에 숨겨져 있습니다"가 "예"여야 합니다.이렇게 하면 화면 상단에 파란색 막대가 추가로 표시될 때 이미지가 왜곡되는 것을 방지할 수 있습니다.
iOS 13 및 Swift 5용으로 업데이트됨
위의 답변 중 어느 것도 도움이 되지 않는 경우.다음 항목이 있는지 목록을 확인하십시오.
"컨트롤러 기반 상태 표시줄 표시"
이 경우 반드시 [YES]로 설정해 주세요.!!
그러면 다음 코드가 작동합니다.
override var prefersStatusBarHidden: Bool {
return true
}
내게 적합한 솔루션. 로드 중에 특정 뷰 컨트롤러의 상태 표시줄을 숨기려면 다음 절차를 따릅니다.
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()
}
}
주의: 정보에서 키 "View controller-based status bar apaurance"를 "NO"로 설정하면 위의 코드가 작동하지 않습니다.키를 "YES"로 설정하거나 info.plist에서 삭제해야 합니다.
프로젝트의 [General]-> [ Deployment Info ]-> [ Status bar style ]에서 [Hide status](상태바 숨기기) 체크마크를 선택합니다.주의: 어플리케이션 전체에서 상태바를 숨깁니다.
뷰 컨트롤러를 모듈식으로 표시하는 경우
viewController.hidesBottomBarWhenPushed = true
viewController.modalPresentationCapturesStatusBarAppearance = true
Xcode 8.1(8B62)을 사용하고 있으며, 도입 타겟은 10.1로 설정되어 있습니다.위의 오버라이드 옵션에서는 그다지 운이 없었습니다.그러나 Deployment Info의 "Hide status bar(상태 표시줄 숨기기)" 옵션을 선택하면 문제가 해결되었습니다.
이게 도움이 됐으면 좋겠어요.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.isStatusBarHidden = true
return true
}
는, 「 」로 할 수 .ViewController
Class
scope
open override var prefersStatusBarHidden: Bool { return true }
프로젝트 내 -> 일반 -> 도입 정보
상태 표시줄 스타일:--
상태 표시줄 숨기기(iOS 10)로 표시됨
스위프트 4
//MARK:- Show Status Bar
UIApplication.shared.isStatusBarHidden = false
//MARK:- Hide Status Bar
UIApplication.shared.isStatusBarHidden = true
언급URL : https://stackoverflow.com/questions/24236912/how-do-i-hide-the-status-bar-in-a-swift-ios-app
'programing' 카테고리의 다른 글
Posh-Git에서 "git status" 출력 색상 변경 (0) | 2023.04.17 |
---|---|
PowerShell의 기본 출력 인코딩을 UTF-8로 변경 (0) | 2023.04.17 |
PowerShell 스크립트를 사용하여 EXE 파일 실행 (0) | 2023.04.17 |
지정된 디렉토리에서 파일만 찾고 bash를 사용하여 하위 디렉토리를 무시하는 방법 (0) | 2023.04.17 |
조건의 Bash에서 "unary operator expected" 오류가 발생했습니다. (0) | 2023.04.17 |