Swift setToolbarHidden不起作用

苏梅

我正在处理Udacity的Memeapp项目,并尝试在截屏之前隐藏导航栏和工具栏。但是,setToolbarHidden似乎不起作用。屏幕截图仍然具有工具栏。我的代码是这样的:

func generateMemedImage() -> UIImage {
    navigationController?.setToolbarHidden(true, animated: true)
    navigationController?.setNavigationBarHidden(true, animated: true)

    UIGraphicsBeginImageContext(self.view.frame.size)
    view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
    let memedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    navigationController?.setNavigationBarHidden(false, animated: true)
    navigationController?.setToolbarHidden(false, animated: true)

    return memedImage
}

这里的任何帮助都非常感谢,谢谢!

编辑:

我的导航栏和工具栏都添加到情节提要中。

请在此处查看屏幕截图。

并且,导航面板在这里。

瓦西里·博德纳丘克(Vasily Bodnarchuk)

试试这个:

toolBar.isHidden = newValue
tabBar.isHidden = newValue

该示例将创建屏幕快照并将其显示。

完整样本

Extensions.swift

extension UIApplication {

    var screenShot: UIImage?  {

        let layer = keyWindow!.layer
        let scale = UIScreen.main.scale

        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let screenshot = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return screenshot
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var dateLabel: UILabel!

    @IBOutlet weak var toolBar: UIToolbar!
    @IBOutlet weak var tabBar: UITabBar!

    private var _barsAreHidden = false
    var barsAreHidden:Bool {
        set (newValue) {
            //navigationController?.setToolbarHidden(newValue, animated: true)
            navigationController?.setNavigationBarHidden(newValue, animated: true)
            toolBar.isHidden = newValue
            tabBar.isHidden = newValue
        }

        get {
            return _barsAreHidden
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        barsAreHidden = false
        dateLabel.text = nil
    }

    func generateMemedImage() -> UIImage? {
        barsAreHidden = true
        let screenShot = UIApplication.shared.screenShot
        barsAreHidden = false
        return screenShot
    }

    @IBAction func makeScreenShot(_ sender: Any) {
        dateLabel.text = "\(NSDate())"
        if let vc = storyboard?.instantiateViewController(withIdentifier: "ScreenShotDispalyViewController") as? ScreenShotDispalyViewController {
            vc.screenShotImage = generateMemedImage()
            navigationController?.pushViewController(vc, animated: true)
        }
        dateLabel.text = nil

    }

    @IBAction func hideAllBars(_ sender: Any) {
        barsAreHidden = true
        DispatchQueue.global(qos: .utility).async {
            sleep(2)
            DispatchQueue.main.async {
                self.barsAreHidden = false
            }
        }
    }
}

ScreenShotDispalyViewController.swift

import UIKit

class ScreenShotDispalyViewController: UIViewController {

    @IBOutlet weak var screenShotView: UIImageView!

    var screenShotImage: UIImage? 

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setToolbarHidden(true, animated: true)
        screenShotView.image = screenShotImage
    }

}

主板

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="QTa-p8-07R">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
        <capability name="Constraints to layout margins" minToolsVersion="6.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackoverflow_40949933" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="zbR-st-Zkt">
                                <rect key="frame" x="-1" y="323" width="376" height="21"/>
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <tabBar contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Dpj-QU-kbw">
                                <rect key="frame" x="0.0" y="618" width="375" height="49"/>
                                <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
                                <items>
                                    <tabBarItem systemItem="favorites" id="WkP-A3-0Vc"/>
                                    <tabBarItem systemItem="more" id="81b-9F-2KD"/>
                                </items>
                            </tabBar>
                            <toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="0hT-0f-jdQ">
                                <rect key="frame" x="1" y="556" width="375" height="44"/>
                                <items>
                                    <barButtonItem title="Item" id="tfX-1b-ih3"/>
                                </items>
                            </toolbar>
                        </subviews>
                        <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="0hT-0f-jdQ" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="-15" id="8N8-9Z-DX3"/>
                            <constraint firstItem="Dpj-QU-kbw" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="9nc-Wr-VAM"/>
                            <constraint firstItem="Dpj-QU-kbw" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="AiM-jN-NKi"/>
                            <constraint firstAttribute="trailing" secondItem="zbR-st-Zkt" secondAttribute="trailing" id="Ga2-j4-QoN"/>
                            <constraint firstItem="zbR-st-Zkt" firstAttribute="centerX" secondItem="Dpj-QU-kbw" secondAttribute="centerX" id="Jni-7E-zYV"/>
                            <constraint firstItem="zbR-st-Zkt" firstAttribute="trailing" secondItem="Dpj-QU-kbw" secondAttribute="trailing" id="R6Q-JU-ezC"/>
                            <constraint firstItem="Dpj-QU-kbw" firstAttribute="top" secondItem="0hT-0f-jdQ" secondAttribute="bottom" constant="18" id="rO1-6C-bfo"/>
                            <constraint firstItem="zbR-st-Zkt" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="vlP-lE-CfP"/>
                            <constraint firstAttribute="trailingMargin" secondItem="0hT-0f-jdQ" secondAttribute="trailing" constant="-17" id="zCl-ym-Ozi"/>
                        </constraints>
                    </view>
                    <navigationItem key="navigationItem" id="OWi-Si-dJ8">
                        <barButtonItem key="leftBarButtonItem" style="plain" id="NSu-0Y-F6Z">
                            <button key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="t39-rx-C6w">
                                <rect key="frame" x="16" y="7" width="83" height="30"/>
                                <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                <state key="normal" title="HideAll"/>
                                <connections>
                                    <action selector="hideAllBars:" destination="BYZ-38-t0r" eventType="touchUpInside" id="63T-fm-Ap3"/>
                                </connections>
                            </button>
                        </barButtonItem>
                        <barButtonItem key="rightBarButtonItem" title="ScreenShot" id="dQ2-9m-icX">
                            <button key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Yf7-f6-hlv">
                                <rect key="frame" x="276" y="7" width="83" height="30"/>
                                <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                <state key="normal" title="ScreenShot"/>
                                <connections>
                                    <action selector="makeScreenShot:" destination="BYZ-38-t0r" eventType="touchUpInside" id="kx3-vG-GTt"/>
                                </connections>
                            </button>
                        </barButtonItem>
                    </navigationItem>
                    <connections>
                        <outlet property="dateLabel" destination="zbR-st-Zkt" id="kpB-Cr-v6I"/>
                        <outlet property="tabBar" destination="Dpj-QU-kbw" id="YcD-Zr-zpm"/>
                        <outlet property="toolBar" destination="0hT-0f-jdQ" id="tdk-Xc-I7O"/>
                        <segue destination="ymc-dM-5SJ" kind="show" identifier="toScreenShotDispalyViewController" id="aJO-Nk-BGg"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="1071.2" y="137.18140929535232"/>
        </scene>
        <!--Screen Shot Dispaly View Controller-->
        <scene sceneID="W1a-69-l1F">
            <objects>
                <viewController storyboardIdentifier="ScreenShotDispalyViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="ymc-dM-5SJ" customClass="ScreenShotDispalyViewController" customModule="stackoverflow_40949933" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="UPX-vz-TxQ"/>
                        <viewControllerLayoutGuide type="bottom" id="SCl-Ty-ZAv"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="Im2-Ro-q7m">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="ZH8-h8-LKb">
                                <rect key="frame" x="0.0" y="64" width="375" height="603"/>
                                <color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
                            </imageView>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                        <constraints>
                            <constraint firstAttribute="trailing" secondItem="ZH8-h8-LKb" secondAttribute="trailing" id="2Lt-IE-fya"/>
                            <constraint firstItem="ZH8-h8-LKb" firstAttribute="leading" secondItem="Im2-Ro-q7m" secondAttribute="leading" id="E7b-gK-1Wy"/>
                            <constraint firstItem="ZH8-h8-LKb" firstAttribute="top" secondItem="UPX-vz-TxQ" secondAttribute="bottom" id="KhE-eL-Gia"/>
                            <constraint firstItem="ZH8-h8-LKb" firstAttribute="bottom" secondItem="SCl-Ty-ZAv" secondAttribute="top" id="rMk-ZS-1pf"/>
                        </constraints>
                    </view>
                    <connections>
                        <outlet property="screenShotView" destination="ZH8-h8-LKb" id="3Nd-Dr-Cbt"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="jkX-Yt-dtS" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="1870" y="137"/>
        </scene>
        <!--Navigation Controller-->
        <scene sceneID="gon-W0-j8h">
            <objects>
                <navigationController automaticallyAdjustsScrollViewInsets="NO" id="QTa-p8-07R" sceneMemberID="viewController">
                    <toolbarItems/>
                    <navigationBar key="navigationBar" contentMode="scaleToFill" id="gR4-pe-CNH">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
                        <autoresizingMask key="autoresizingMask"/>
                    </navigationBar>
                    <nil name="viewControllers"/>
                    <connections>
                        <segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="sxR-Xd-KcW"/>
                    </connections>
                </navigationController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="03f-Oc-BF2" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="61.600000000000001" y="-545.57721139430294"/>
        </scene>
    </scenes>
</document>

结果

在此处输入图片说明 在此处输入图片说明

细节

xCode 8.1,快速3

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Swift NSCoding不起作用

来自分类Dev

KeyDown不起作用,Swift

来自分类Dev

MPMoviePlayerController不起作用-Swift

来自分类Dev

Swift SequenceType不起作用

来自分类Dev

Swift NSEvent不起作用

来自分类Dev

Swift for循环不起作用

来自分类Dev

searchBarSearchButtonClicked不起作用Swift

来自分类Dev

Swift 请求不起作用

来自分类Dev

Swift 协议不起作用

来自分类Dev

Swift-UIActionSheet事件不起作用

来自分类Dev

Swift UIApplication.setStatusBarStyle不起作用

来自分类Dev

Swift 2.2,包含方法不起作用

来自分类Dev

ADInterstitialAd在iPad Swift上不起作用

来自分类Dev

Swift-AVAudioPlayer不起作用

来自分类Dev

scheduleLocalNotification在Swift 2.0中不起作用

来自分类Dev

Swift-Switch语句不起作用

来自分类Dev

Swift泛型函数不起作用

来自分类Dev

关闭nsviewcontroller swift不起作用

来自分类Dev

Swift中的可选分配不起作用?

来自分类Dev

UITextField inputView在Swift中不起作用

来自分类Dev

功能在Swift中不起作用

来自分类Dev

UIAlertView在Swift中不起作用

来自分类Dev

Swift UIButton覆盖,setImage不起作用

来自分类Dev

在Swift中创建块不起作用

来自分类Dev

Swift CoreData子类不起作用

来自分类Dev

perfrmSegueWithIdentifier()在Swift iOS中不起作用

来自分类Dev

Swift:按钮启用不起作用?

来自分类Dev

presentViewController在Swift中不起作用

来自分类Dev

UIDevice在Swift 2.0中不起作用