隐藏后,状态栏仍显示为黑色

时间:2016-08-31 04:21:01

标签: swift xcode hidden statusbar

我的状态栏在按下按钮时消失,并且所有作品都隐藏起来并且不隐藏在树上但是当我回到最后一个视图控制器时,白色状态栏隐藏但仍留下黑色状态栏显示我正在使用按下按钮时使用的相同代码,因为我在视野中会消失。

以下是使用的代码:

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

@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)

非常感谢任何帮助。

一切顺利 Tony Merritt

1 个答案:

答案 0 :(得分:0)

一些术语

状态栏:

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>