Blame view

HDFwear/Tools/UIViewController+Extension.swift 3.47 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  //
  //  UIViewController+Extension.swift
  //  zc
  //
  //  Created by wyp on 2020/10/22.
  //  Copyright © 2020 wyp. All rights reserved.
  //
  
  import UIKit
  
  public extension UIViewController {
      
      func showAlert(title: String, message: String, confirm: ((UIAlertAction) -> Void)? = nil) {
          showAlert(title: title, message: message, confirm: confirm, cancel: nil)
      }
      
      func showAlert(title: String, message: String, confirmText: String = "确定", cancelText: String? = "取消", confirm: ((UIAlertAction) -> Void)? = nil, cancel: ((UIAlertAction) -> Void)? = nil) {
          var conText = confirmText
          var canText = cancelText
          if confirmText == "确定" {
              conText = LocString("确定")
          }
          if cancelText == "取消" {
              canText = LocString("取消")
          }
      
          let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
          if canText != nil {
              let cancelAction = UIAlertAction(title: canText, style: .default, handler: cancel)
              cancelAction.setValue(UIColor.rgbColorFromHex(0x808080), forKey: "titleTextColor")
              alertVC.addAction(cancelAction)
          }
          
          let confirmAction = UIAlertAction(title: conText, style: .destructive, handler: confirm)
          confirmAction.setValue(UIColor.rgbColorFromHex(0x00993E), forKey: "titleTextColor")
          alertVC.addAction(confirmAction)
  
          present(alertVC, animated: true, completion: nil)
      }
      
  
      class func getCurrentViewController() -> UIViewController?{
          // 获取当先显示的window
          var currentWindow = UIApplication.shared.keyWindow ?? UIWindow()
          if currentWindow.windowLevel != UIWindow.Level.normal {
              let windowArr = UIApplication.shared.windows
              for window in windowArr {
                  if window.windowLevel == UIWindow.Level.normal {
                      currentWindow = window
                      break
                  }
              }
          }
          return UIViewController.getNextXController(nextController: currentWindow.rootViewController)
      }
      
      private class func  getNextXController(nextController: UIViewController?) -> UIViewController? {
          if nextController == nil {
              return nil
          } else if nextController?.presentedViewController != nil {
              return UIViewController.getNextXController(nextController: nextController?.presentedViewController)
          } else if let tabbar = nextController as? UITabBarController {
              return UIViewController.getNextXController(nextController: tabbar.selectedViewController)
          } else if let nav = nextController as? UINavigationController {
              return UIViewController.getNextXController(nextController: nav.visibleViewController)
          }
          return nextController
      }
  
  }
  
  
  extension NSLayoutConstraint {
  
  
      func setMultiplier(multiplier:CGFloat) -> NSLayoutConstraint {
  
          NSLayoutConstraint.deactivate([self])
  
          let newConstraint = NSLayoutConstraint(
              item: firstItem,
              attribute: firstAttribute,
  
              relatedBy: relation,
  
              toItem: secondItem,
  
              attribute: secondAttribute,
  
              multiplier: multiplier,
  
              constant: constant)
  
          newConstraint.priority = priority
  
          newConstraint.shouldBeArchived = shouldBeArchived
  
          newConstraint.identifier = identifier
  
  
  
          NSLayoutConstraint.activate([newConstraint])
  
          return newConstraint
  
      }
  
  }