Last active
May 13, 2016 02:17
-
-
Save ateliercw/12c158ab25b32df1ebdb5ccc6c583d5e to your computer and use it in GitHub Desktop.
Showing UIAppearance Proxies on on values that aren't normally exposed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private extension AppStyle { | |
func setupAppearance() { | |
setupNavBarAppearance() | |
setupTableViewCellAppearance() | |
setupTableAppearance() | |
} | |
func setupNavBarAppearance() { | |
let navBarAppearance = UINavigationBar.appearanceWhenContainedInInstancesOfClasses([NavigationController.self]) | |
navBarAppearance.translucent = false | |
navBarAppearance.shadowImage = UIImage.solidImage(color: colors.tintColor) | |
let barImage = UIImage.solidImage(color: colors.navigationBackgroundColor) | |
navBarAppearance.setBackgroundImage(barImage, forBarMetrics: .Default) | |
let textStyle: [String:AnyObject] = [ | |
NSForegroundColorAttributeName: colors.navbarTextColor | |
] | |
navBarAppearance.titleTextAttributes = textStyle | |
} | |
func setupTableViewCellAppearance() { | |
let cellAppearance = UITableViewCell.appearanceWhenContainedInInstancesOfClasses([NavigationController.self]) | |
cellAppearance.backgroundColor = colors.backgroundColor | |
let selectedView = UIView() | |
selectedView.backgroundColor = colors.selectedBackgroundColor | |
cellAppearance.selectedBackgroundView = selectedView | |
cellAppearance.forceInsets(UIEdgeInsets()) | |
let nestedClasses: [AnyObject.Type] = [UITableViewCell.self, NavigationController.self] | |
let cellLabelAppearance = UILabel.appearanceWhenContainedInInstancesOfClasses(nestedClasses) | |
cellLabelAppearance.forceFont(UIFont.preferredFontForTextStyle(UIFontTextStyleBody)) | |
cellLabelAppearance.highlightedTextColor = colors.selectedTextColor | |
cellLabelAppearance.forceTextColor(colors.textColor) | |
} | |
func setupTableAppearance() { | |
let tableViewAppearance = UITableView.appearanceWhenContainedInInstancesOfClasses([NavigationController.self]) | |
tableViewAppearance.backgroundColor = colors.backgroundColor | |
tableViewAppearance.tableFooterView = UIView() | |
tableViewAppearance.separatorColor = colors.textColor | |
tableViewAppearance.forceInsets(UIEdgeInsets()) | |
} | |
} | |
//MARK: - Functions to force configuration of properties not exposed to UIApperarance by default | |
private extension UILabel { | |
@objc func forceTextColor(color: UIColor) { | |
textColor = color | |
} | |
@objc func forceFont(font: UIFont) { | |
self.font = font | |
} | |
} | |
private extension UITableView { | |
@objc func forceInsets(edgeInsets: UIEdgeInsets) { | |
separatorInset = edgeInsets | |
layoutMargins = edgeInsets | |
} | |
} | |
private extension UITableViewCell { | |
@objc func forceInsets(edgeInsets: UIEdgeInsets) { | |
separatorInset = edgeInsets | |
layoutMargins = edgeInsets | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment