UIViewController+Extension.swift 3.47 KB
//
//  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

    }

}