正在研究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
}
非常感谢任何帮助,谢谢!
编辑:
导航栏和工具栏都添加在故事板中。
答案 0 :(得分:0)
试试这个:
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
}
}
Main.storyboard
<?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,swift 3