Swift iOS : tintColor

RecoReco發表於2017-07-06

在iOS 7後,UIView新增加了一個tintColor屬性,這個屬性表示的是“色調”,一旦設定顏色給它,那麼以此檢視為根檢視的整個檢視層次結構都會被修改顏色。從而可以不必一一賦值就可以在檢視體系內得到一致的顏色體系。

如下程式碼當點選RUN按鈕時,整個以self.view為根檢視的檢視體系內的所有檢視的顏色都會跟著在紅黃藍三種色調內切換:

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let page = Page1()
        self.window!.rootViewController = page
        self.window?.makeKeyAndVisible()
        return true
    }
}
class Page1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        let f = FrameView(frame: view.frame)

        self.view.addSubview(f)

    }
}
class FrameView:UIView{
    var label : UILabel!
    var count = 0
    override func tintColorDidChange() {
        self.label.textColor = self.tintColor;
    }
    func buttonAction(_ sender:UIButton!){
        self.tintColor = [UIColor.red,UIColor.yellow,UIColor.blue][count]
        count += 1
        if count == 3{
            count = 0
        }
    }
    override init(frame: CGRect) {
        super.init(frame:frame)
        label   = UILabel()
        label.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
        label.text =  "label tint color"
        label.textAlignment = .left
        self.addSubview(label)
        let button   = UIButton(type: .system)
        button.frame = CGRect(x: 120, y: 150, width: 200, height: 50)
        button.setTitle("RUN",for: .normal)
        button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
        self.addSubview(button)
        let s   = UISlider()
        s.frame = CGRect(x: 120, y: 200, width: 200, height: 50)
        s.value =  0.5
        self.addSubview(s)
        let p   = UIProgressView()
        p.frame = CGRect(x: 120, y: 250, width: 200, height: 50)
        p.progress =  0.5
        self.addSubview(p)
        let stper   = UIStepper()
        stper.frame = CGRect(x: 120, y: 300, width: 200, height: 50)
        self.addSubview(stper)
        let imageView = UIImageView()
        imageView.image = UIImage.imageWithColor(.red)
        imageView.image = imageView.image?.withRenderingMode(.alwaysTemplate)
        imageView.frame = CGRect(x: 120, y: 350, width: 200, height: 50)
        self.addSubview(imageView)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension UIImage {
    class func imageWithColor(_ color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width: 10.0,height: 10.0 )
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}複製程式碼

只有一個除外,就是label。它是不會隨著上級檢視的tint修改而跟著改的。如果想要跟著修改,那麼需要覆蓋方法:

override func tintColorDidChange()複製程式碼

自己設定此顏色給label。
還有一個做了特殊處理的檢視,是ImageView。圖片都是固定的圖,如何根據色調的變化而跟著修改顏色呢?

設定圖片的imageWithRenderingMode屬性為AlwaysTemplate,這樣渲染圖片時會將其渲染為一個模板而忽略它的顏色資訊,這意味著,對一個畫素而言,如果它的alpha值為1的話,就將它的顏色設定為tint color;如果不為1的話,則設定為透明的。

廣告

Swift iOS開發小書 epub.ituring.com.cn/946

相關文章