标签:swift主题色 tabbar navigationbar uitabbarcontroller uinavigationbarcontr
一、常规主题色使用点extension UIColor { //主题色 class func applicationMainColor() -> UIColor { return UIColor(red: 238/255, green: 64/255, blue: 86/255, alpha:1) } //第二主题色 class func applicationSecondColor() -> UIColor { return UIColor.lightGrayColor() } //警告颜色 class func applicationWarningColor() -> UIColor { return UIColor(red: 0.1, green: 1, blue: 0, alpha: 1) } //链接颜色 class func applicationLinkColor() -> UIColor { return UIColor(red: 59/255, green: 89/255, blue: 152/255, alpha:1) } }
func configTabBar() { let items = self.tabBar.items for item in items as [UITabBarItem] { let dic = NSDictionary(object: UIColor.applicationMainColor(), forKey: NSForegroundColorAttributeName) item.setTitleTextAttributes(dic, forState: UIControlState.Selected) } }
self.tabBar.selectedImageTintColor = UIColor.applicationMainColor()
let imageNormal = UIImage(contentsOfFile: "imageNormal")?. imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) let imageSelected = UIImage(contentsOfFile: "imageSelected") let tabBarItem = UITabBarItem(title: "title", image: imageNormal, selectedImage: imageSelected)
extension UIViewController { func viewDidLoadForChangeTitleColor() { self.viewDidLoadForChangeTitleColor() if self.isKindOfClass(UINavigationController.classForCoder()) { self.changeNavigationBarTextColor(self as UINavigationController) } } func changeNavigationBarTextColor(navController: UINavigationController) { let nav = navController as UINavigationController let dic = NSDictionary(object: UIColor.applicationMainColor(), forKey:NSForegroundColorAttributeName) nav.navigationBar.titleTextAttributes = dic nav.navigationBar.barTintColor = UIColor.applicationSecondColor() nav.navigationBar.tintColor = UIColor.applicationMainColor() } }
func swizzlingMethod(clzz: AnyClass, #oldSelector: Selector, #newSelector: Selector) { let oldMethod = class_getInstanceMethod(clzz, oldSelector) let newMethod = class_getInstanceMethod(clzz, newSelector) method_exchangeImplementations(oldMethod, newMethod) }
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { swizzlingMethod(UIViewController.self, oldSelector: "viewDidLoad", newSelector: "viewDidLoadForChangeTitleColor") //do others return true }
self.viewDidLoadForChangeTitleColor()是不会造成循环调用,反而是调用了我们期望执行的viewDidLoad方法体。
extension UILabel { var colorString: String { set(newValue) { switch newValue { case "main": self.textColor = UIColor.applicationMainColor() case "second": self.textColor = UIColor.applicationSecondColor() case "warning": self.textColor = UIColor.applicationWarningColor() default: self.textColor = UIColor.applicationSecondColor() } } get { return self.colorString } } }
4.总结
标签:swift主题色 tabbar navigationbar uitabbarcontroller uinavigationbarcontr
原文地址:http://blog.csdn.net/g1jun/article/details/41543885