swift - 隐藏后状态栏仍然显示为黑色

标签 swift xcode hidden statusbar

我的状态栏在按下按钮时消失,所有工作都在树上隐藏和取消隐藏,但是当我返回到最后一个 View Controller 时,白色状态栏隐藏,但仍然留下黑色状态栏,显示我正在使用与我在 View 中按下的按钮隐藏相同的代码将消失。

这里是使用的代码:

//按下按钮可以隐藏白色状态栏,下面没有黑色状态栏。

@IBAction func onPlayPressed(sender: AnyObject) {
    start()


    isHidden = !isHidden
    UIView.animateWithDuration(0.3) { () -> Void in
        self.setNeedsStatusBarAppearanceUpdate()

}
    self.navigationController?.hidesBarsOnTap = false
    self.navigationController?.setNavigationBarHidden(true, animated: true)

//此部分隐藏白色状态栏,但在下面留下黑色状态栏。

override func viewWillDisappear(animated: Bool) {
    synth.pause()
    vox.pause()

    isHidden = true
    UIView.animateWithDuration(0.3) { () -> Void in
        self.setNeedsStatusBarAppearanceUpdate()

    }
    self.navigationController?.hidesBarsOnTap = false
    self.navigationController?.setNavigationBarHidden(true, animated: true)

任何帮助将不胜感激。

祝一切顺利 托尼·梅里特

最佳答案

一些术语

状态栏:

enter image description here

导航栏:

enter image description here

导航和状态栏:

enter image description here

尝试我的示例:

ViewController.swift

import UIKit

class ViewController: UIViewController {

@IBOutlet var hideButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    if (self.navigationController?.navigationBarHidden)! {
        hideButton.setTitle("show", forState: .Normal)
    }
}

@IBAction func hideNavigationBar(sender: AnyObject) {
    self.navigationController?.hidesBarsOnTap = false
    self.navigationController?.setNavigationBarHidden(!(self.navigationController?.navigationBarHidden)!, animated: true)
}

@IBAction func goBack(sender: AnyObject) {
    self.navigationController?.popViewControllerAnimated(true)
}
}

Main.storyboard

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="T9D-e2-4rQ">
<dependencies>
    <deployment identifier="iOS"/>
    <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
    <!--View Controller-->
    <scene sceneID="ZDJ-xV-lml">
        <objects>
            <viewController id="3W3-SS-KDU" sceneMemberID="viewController">
                <layoutGuides>
                    <viewControllerLayoutGuide type="top" id="Mxj-5M-2iY"/>
                    <viewControllerLayoutGuide type="bottom" id="3C4-b8-R1X"/>
                </layoutGuides>
                <view key="view" contentMode="scaleToFill" id="Hr5-jU-7uD">
                    <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <subviews>
                        <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DLX-GN-6Nd">
                            <rect key="frame" x="283" y="285" width="34" height="30"/>
                            <state key="normal" title="push"/>
                            <connections>
                                <segue destination="aX5-Sj-vzs" kind="show" id="5hq-PZ-fji"/>
                            </connections>
                        </button>
                    </subviews>
                    <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                    <constraints>
                        <constraint firstItem="DLX-GN-6Nd" firstAttribute="centerY" secondItem="Hr5-jU-7uD" secondAttribute="centerY" id="hM7-eb-ZcS"/>
                        <constraint firstItem="DLX-GN-6Nd" firstAttribute="centerX" secondItem="Hr5-jU-7uD" secondAttribute="centerX" id="yTt-om-iNF"/>
                    </constraints>
                </view>
                <navigationItem key="navigationItem" id="PzV-EJ-7im"/>
            </viewController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="GKQ-Ot-Zmx" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="865" y="375"/>
    </scene>
    <!--View Controller-->
    <scene sceneID="dHO-Tf-TVb">
        <objects>
            <viewController id="aX5-Sj-vzs" customClass="ViewController" customModule="stackoverflow_39240675" customModuleProvider="target" sceneMemberID="viewController">
                <layoutGuides>
                    <viewControllerLayoutGuide type="top" id="a3I-YL-ZHX"/>
                    <viewControllerLayoutGuide type="bottom" id="EDl-yb-Y7O"/>
                </layoutGuides>
                <view key="view" contentMode="scaleToFill" id="dve-Qr-cm1">
                    <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <subviews>
                        <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XYd-b4-iN3">
                            <rect key="frame" x="285" y="285" width="30" height="30"/>
                            <state key="normal" title="hide">
                                <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                            </state>
                            <connections>
                                <action selector="hideNavigationBar:" destination="aX5-Sj-vzs" eventType="touchUpInside" id="8OZ-fj-hkz"/>
                            </connections>
                        </button>
                        <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="KHT-Tm-0Sw">
                            <rect key="frame" x="283" y="323" width="34" height="30"/>
                            <state key="normal" title="back">
                                <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                            </state>
                            <connections>
                                <action selector="goBack:" destination="aX5-Sj-vzs" eventType="touchUpInside" id="0Gj-6r-RH0"/>
                            </connections>
                        </button>
                    </subviews>
                    <color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
                    <constraints>
                        <constraint firstItem="XYd-b4-iN3" firstAttribute="centerY" secondItem="dve-Qr-cm1" secondAttribute="centerY" id="4oU-lu-cib"/>
                        <constraint firstItem="KHT-Tm-0Sw" firstAttribute="centerX" secondItem="XYd-b4-iN3" secondAttribute="centerX" id="DHD-BE-uLP"/>
                        <constraint firstItem="KHT-Tm-0Sw" firstAttribute="top" secondItem="XYd-b4-iN3" secondAttribute="bottom" constant="8" symbolic="YES" id="Mfb-X2-i3d"/>
                        <constraint firstItem="XYd-b4-iN3" firstAttribute="centerX" secondItem="dve-Qr-cm1" secondAttribute="centerX" id="UY9-eH-ABh"/>
                    </constraints>
                </view>
                <connections>
                    <outlet property="hideButton" destination="XYd-b4-iN3" id="uLV-9H-ZwF"/>
                </connections>
            </viewController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="taE-x0-pMJ" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="1525" y="376"/>
    </scene>
    <!--Navigation Controller-->
    <scene sceneID="g8h-An-4td">
        <objects>
            <navigationController automaticallyAdjustsScrollViewInsets="NO" id="T9D-e2-4rQ" sceneMemberID="viewController">
                <toolbarItems/>
                <navigationBar key="navigationBar" contentMode="scaleToFill" id="8Zu-zN-61c">
                    <rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
                    <autoresizingMask key="autoresizingMask"/>
                </navigationBar>
                <nil name="viewControllers"/>
                <connections>
                    <segue destination="3W3-SS-KDU" kind="relationship" relationship="rootViewController" id="P8d-UH-2I1"/>
                </connections>
            </navigationController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="8cC-0q-iMj" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="53" y="375"/>
    </scene>
</scenes>
</document>

关于swift - 隐藏后状态栏仍然显示为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39240675/

相关文章:

ios - 如何解决xcode中的循环依赖错误

ios - CocoaPods iOS 框架链接 i386 文件

ios - 重新启动 Xcode 后找不到 DropboxSDK.framework 文件

css - 完全隐藏隐藏了一些溢出的元素

IOS Swift,无法使用 NSPredicate 过滤 PHFetchOptions 中的隐藏照片

SwiftUI:图像异步更改后 View 不会更新

ios - 如何绑定(bind)数组计数来替换按钮图像

jquery - CSS 选择隐藏元素

arrays - 使用 ObjectMapper Swift 3 将 Json 映射到嵌套数组

ios - 如何为 UICollectionView 单元格选择设置动画