Created
July 2, 2017 03:01
-
-
Save ya-s-u/9e732c154111afe8e00823482925c184 to your computer and use it in GitHub Desktop.
UITabBar.unselectedItemTintColorをiOS9以下でも使えるようにする
This file contains 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
import UIKit | |
extension UITabBar { | |
private struct AssociatedKey { | |
static var unselectedItemTintColor = "UITabBar.UnselectedItemTintColor" | |
} | |
@nonobjc | |
var unselectedItemTintColor: UIColor? { | |
get { | |
return objc_getAssociatedObject(self, &AssociatedKey.unselectedItemTintColor) as? UIColor | |
} | |
set { | |
objc_setAssociatedObject(self, &AssociatedKey.unselectedItemTintColor, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
if let color = newValue { | |
setUnselectedItemTintColor(color) | |
} | |
} | |
} | |
private func setUnselectedItemTintColor(_ color: UIColor) { | |
items?.forEach { | |
$0.image = $0.image?.withRenderingMode(.alwaysOriginal) | |
$0.setTitleTextAttributes([NSForegroundColorAttributeName: color], for: .normal) | |
$0.setTitleTextAttributes([NSForegroundColorAttributeName: tintColor], for: .selected) | |
} | |
} | |
} |
This file contains 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
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var tabBar: UITabBar! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tabBar.selectedItem = tabBar.items?.first | |
tabBar.tintColor = .red | |
tabBar.unselectedItemTintColor = .blue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment