swift – 动画导航栏颜色

前端之家收集整理的这篇文章主要介绍了swift – 动画导航栏颜色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我弹回到前一个控制器时,我试图设置导航栏颜色的变化.为了给它一些上下文,我有一个控制器A,它是一个collectionView控制器,并有一个不透明的导航栏颜色设置:
  1. self.navigationController?.navigationBar.barTintColor = UIColor.rgb(red: 244,green: 67,blue: 54)
  2. self.navigationController?.navigationBar.tintColor = .white
  3. self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
  4. self.navigationController?.navigationBar.isTranslucent = false

一旦选择了collectionViewCell,我就会推送到下一个控制器B,导航栏被改变为透明:

  1. self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
  2. self.navigationController?.navigationBar.shadowImage = UIImage()
  3. self.navigationController?.navigationBar.isTranslucent = true

用户点击后箭头时,我希望navigationBar返回其原始颜色.我在控制器B的viewWillDissappear函数,willMove(toParentViewController)和Controller A上的viewWillAppear函数上尝试了一个简单的UIView动画:

这是动画:

  1. UIView.animate(withDuration: 0.5) {
  2. self.navigationController?.navigationBar.barTintColor = UIColor.rgb(red: 244,blue: 54)
  3. self.navigationController?.navigationBar.setBackgroundImage(nil,for: .default)
  4. self.navigationController?.navigationBar.shadowImage = nil
  5. self.navigationController?.navigationBar.isTranslucent = false
  6. self.navigationController?.navigationBar.layoutIfNeeded()
  7. }

在这之后,我尝试使用转换协调器,但得到了相同的结果:

  1. guard let coordinator = self.transitionCoordinator else {
  2. return
  3. }
  4.  
  5. coordinator.animate(alongsideTransition: {
  6. [weak self] context in
  7. self?.navigationController?.navigationBar.barTintColor = UIColor.rgb(red: 244,blue: 54)
  8. self?.navigationController?.navigationBar.setBackgroundImage(nil,for: .default)
  9. self?.navigationController?.navigationBar.shadowImage = nil
  10. self?.navigationController?.navigationBar.isTranslucent = false
  11. self?.navigationController?.navigationBar.layoutIfNeeded()
  12. },completion: nil)

似乎无论我尝试什么,或者我把代码放在哪里,我总会得到相同的结果.我知道代码是重复的,但我只是想弄清楚原因,所以发生了很多复制和粘贴.

据我所知,我认为它与前一个控制器的背景视图有关,但我很难过,我似乎在导航栏下的动画之前看到一个黑屏.任何帮助将不胜感激.

谢谢

细节

xCode 8.3.2,swift 3.1

  1. override func viewWillAppear(_ animated: Bool) {
  2. if let navigationBar = self.navigationController?.navigationBar {
  3. navigationBar.backgroundColor = .blue
  4. }
  5. }

完整样本

ViewController

  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5. override func viewWillAppear(_ animated: Bool) {
  6. if let navigationBar = self.navigationController?.navigationBar {
  7. navigationBar.barTintColor = UIColor(red: 244/255,green: 67/255,blue: 54/255,alpha: 1.0)
  8. navigationBar.tintColor = .white
  9. navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
  10. navigationBar.isTranslucent = false
  11. }
  12. }
  13. }

ViewController2

  1. import UIKit
  2.  
  3. class ViewController2: UIViewController {
  4.  
  5. override func viewWillAppear(_ animated: Bool) {
  6. if let navigationBar = self.navigationController?.navigationBar {
  7. let color = UIColor(red: 1,green: 153/255,blue: 0,alpha: 1.0)
  8. navigationBar.setBackgroundImage(UIImage.imageWithColor(color: color),for: .default)
  9. navigationBar.shadowImage = UIImage()
  10. navigationBar.isTranslucent = true
  11. }
  12. }
  13.  
  14. override func viewWillDisappear(_ animated: Bool) {
  15. if let navigationBar = self.navigationController?.navigationBar {
  16. navigationBar.setBackgroundImage(nil,for: .default)
  17. navigationBar.shadowImage = nil
  18. navigationBar.isTranslucent = false
  19. }
  20. }
  21. }

extension UIImage

  1. import UIKit
  2.  
  3. extension UIImage {
  4. class func imageWithColor(color: UIColor) -> UIImage {
  5. let rect = CGRect(x: 0,y: 0,width: 1,height: 1)
  6. UIGraphicsBeginImageContextWithOptions(CGSize(width: 1,height: 1),false,0)
  7. color.setFill()
  8. UIRectFill(rect)
  9. let image = UIGraphicsGetImageFromCurrentImageContext()!
  10. UIGraphicsEndImageContext()
  11. return image
  12. }
  13. }

Main.storyboard

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Tzy-ol-uu0">
  3. <device id="retina4_7" orientation="portrait">
  4. <adaptation id="fullscreen"/>
  5. </device>
  6. <dependencies>
  7. <deployment identifier="iOS"/>
  8. <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
  9. <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
  10. </dependencies>
  11. <scenes>
  12. <!--View Controller-->
  13. <scene sceneID="tne-QT-ifu">
  14. <objects>
  15. <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackowerflow_44343355" customModuleProvider="target" sceneMemberID="viewController">
  16. <layoutGuides>
  17. <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
  18. <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
  19. </layoutGuides>
  20. <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
  21. <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
  22. <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
  23. <subviews>
  24. <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="eIC-Nm-Ex7">
  25. <rect key="frame" x="164" y="318" width="46" height="30"/>
  26. <state key="normal" title="Button"/>
  27. <connections>
  28. <segue destination="QHs-H4-fAS" kind="show" id="Rff-Eq-K6g"/>
  29. </connections>
  30. </button>
  31. </subviews>
  32. <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
  33. <constraints>
  34. <constraint firstItem="eIC-Nm-Ex7" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="G1g-VM-MAn"/>
  35. <constraint firstItem="eIC-Nm-Ex7" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="mdZ-GP-EQw"/>
  36. </constraints>
  37. </view>
  38. <navigationItem key="navigationItem" id="hq3-zt-U4K"/>
  39. </viewController>
  40. <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
  41. </objects>
  42. <point key="canvasLocation" x="977" y="791"/>
  43. </scene>
  44. <!--View Controller2-->
  45. <scene sceneID="F9C-Nz-6dd">
  46. <objects>
  47. <viewController id="QHs-H4-fAS" customClass="ViewController2" customModule="stackowerflow_44343355" customModuleProvider="target" sceneMemberID="viewController">
  48. <layoutGuides>
  49. <viewControllerLayoutGuide type="top" id="AV0-X8-nhX"/>
  50. <viewControllerLayoutGuide type="bottom" id="AsY-Gl-67v"/>
  51. </layoutGuides>
  52. <view key="view" contentMode="scaleToFill" id="1fA-pX-rzR">
  53. <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
  54. <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
  55. <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
  56. </view>
  57. </viewController>
  58. <placeholder placeholderIdentifier="IBFirstResponder" id="Uzd-Tb-KRO" userLabel="First Responder" sceneMemberID="firstResponder"/>
  59. </objects>
  60. <point key="canvasLocation" x="1770" y="789"/>
  61. </scene>
  62. <!--Navigation Controller-->
  63. <scene sceneID="Jff-OO-3e7">
  64. <objects>
  65. <navigationController automaticallyAdjustsScrollViewInsets="NO" id="Tzy-ol-uu0" sceneMemberID="viewController">
  66. <toolbarItems/>
  67. <navigationBar key="navigationBar" contentMode="scaleToFill" id="804-YF-T6T">
  68. <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
  69. <autoresizingMask key="autoresizingMask"/>
  70. </navigationBar>
  71. <nil name="viewControllers"/>
  72. <connections>
  73. <segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="BRB-ym-7I2"/>
  74. </connections>
  75. </navigationController>
  76. <placeholder placeholderIdentifier="IBFirstResponder" id="M6i-ib-61I" userLabel="First Responder" sceneMemberID="firstResponder"/>
  77. </objects>
  78. <point key="canvasLocation" x="140" y="791.15442278860576"/>
  79. </scene>
  80. </scenes>
  81. </document>

猜你在找的Swift相关文章