Can't read titleTextAttributes on appearance for UITabBarItem - ios

I am working on styling the app I'm building. I want to apply some system-wide styling (fonts) to the tab bar and then in some instances style things like colour. I'm running into two problems:
When you set any titleTextAttributes using setTitleTextAttributes:forState: on an instance of a UITabBar, it immediately ignores any titleTextAttributes set on the appearance proxy (including keys that were not set on the instance, but set on the appearance proxy).
// AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UITabBarItem.appearance().setTitleTextAttributes([
NSFontAttributeName: UIFont(name: "Lato-Regular", size: 20.0)!
], forState: .Normal)
}
// Later on in a ViewController that has a UITabBar
override func viewDidLoad() {
myTabBar.setTitleTextAttributes([
NSForegroundColorAttributeName: UIColor.blueColor()
], forState: .Normal)
// Tab bar items now have blue text, but Helvetica Neue font
// We've lost the appearance proxy font (Lato-Regular)
}
In order to fix (1) I was just going to copy the titleTextAttributes from the appearance proxy and then overwrite them with whatever attributes I wanted to apply on the instance. Eg.
// AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UITabBarItem.appearance().setTitleTextAttributes([
NSFontAttributeName: UIFont(name: "Lato-Regular", size: 20.0)!
], forState: .Normal)
}
// Later on in a ViewController that has a UITabBar
override func viewDidLoad() {
var attributes = UITabBarItem.appearance().titleTextAttributesForState(state) ?? [NSObject: AnyObject]()
assert(attributes.count > 0, "Could not read titleTextAttributes set on appearance proxy!")
}
This is most annoying because you can read appearance proxy values on UINavigationBar just fine.
Any ideas?

Related

How to move text field up when keyboard pops up

I am moving in from objective c to swift can anyone specify how to stop keyboard to hide the textfield . The problem is I have some text field in my view controller when the user click on last text field near to bottom of screen the keyboard appears and hide the textfield . is there any easy to solve this or should i have to use scroll view ?
Use this library called IQKeyboardManager:
https://github.com/hackiftekhar/IQKeyboardManager
Install pod file pod 'IQKeyboardManager’ . And in your app delegate
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.enableAutoToolbar = true
IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "Done”
return true
}
}
You can change the following properties of IQKeyboardManager as well
IQKeyboardManager.shared.overrideKeyboardAppearance = true
IQKeyboardManager.shared.keyboardAppearance = .dark
IQKeyboardManager.shared.toolbarBarTintColor = UIColor.cyan
IQKeyboardManager.shared.toolbarDoneBarButtonItemImage = UIImage(named: "<#T##String#>")
IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
IQKeyboardManager.shared.placeholderFont = UIFont(name: "Times New Roman", size: 20.0)
IQKeyboardManager.shared.shouldResignOnTouchOutside = true
IQKeyboardManager.shared.shouldPlayInputClicks = true //default

Eureka Forms: How to change form global tint colour

I am using Eureka to implement a simple form in my project. The form seems to have blue tint in the tint bar buttons above the keyboard and I am unsure how to change it.
Is there a way to make the "Done" label green like the rest of my UI?
Updated Eureka 4.3.x
You should override a variable customNavigationAccessoryView in your controller
override var customNavigationAccessoryView: (UIView & NavigationAccessory)? {
// The width might not be correctly defined yet: (Normally you can use UIScreen.main.bounds)
let navView = NavigationAccessoryView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44.0))
navView.barTintColor = UIColor.MaterialColors.category3
navView.tintColor = UIColor.white
return navView
}
Here you go and fix the colour of Done Label.
override NaviagtionAccessoryView Instance as navigationAccessoryView below
navigationAccessoryView.tintColor = "Your Colour"
navigationAccessoryView.backgroundColor = "Your Colour"
navigationAccessoryView.doneButton.tintColor = "Your Colour"
More Here
You can set a default tint color for all UIView instance by
UIView.appearance().tintColor = .red
appearance() is a method in UIAppearance protocol. UIView and its subclasses confirm to this protocol. You can also do
UIButton.appearance().tintColor = .green
Add the below line in AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
self.window?.tintColor = UIColor.black // You can set it to which color you want.
return true
}
All the tint color of the whole app will be changed to the given color.
This works for me perfectly:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NavigationAccessoryView.appearance().tintColor = /* your color here */
}

iOS UILabel.appearance().textColor is not available

I am trying to change default text color for all labels in my app. I have tried to use UILabel.appearance().textColor, but it's not available for me. Can somebody advise what am I doing wrong? I have checked a lot of questions on SO, the latest one is this. And as per description it's available.
If I changed background color - it works. Tried to change tintColor - no effect. Why? Was it removed from UIAppearance protocol?
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
var smStore: SMStore?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// UIAppearance configuring
let tintColor = UIColor.white
//let backgroundColor = UIColor(red: 5, green: 72, blue: 149, alpha: 1)
let backgroundColor = UIColor.black
// Global tintColor
window?.tintColor = tintColor
// UIViews
UIView.appearance().tintColor = tintColor
UIView.appearance().backgroundColor = backgroundColor
// UINavigationBars
UINavigationBar.appearance().tintColor = tintColor
UINavigationBar.appearance().backgroundColor = tintColor
// UITableViews
UITableView.appearance().backgroundColor = backgroundColor
UITableViewCell.appearance().backgroundColor = backgroundColor
UITableViewCell.appearance().tintColor = tintColor
// UILabels
UILabel.appearance().backgroundColor = UIColor.green
...
}
Xcode 9, Swift 4.
Just type it in. It compiles.
UILabel.appearance().textColor = .red

Globally change UIBarButtonItem text color when selected or highlighted

I have UIToolbar (inside my UINavigationController) with some UIBarButtonItems on it.
Toolbar is blue (isTranslucent = false), button is white. Default highlight color is gray and it looks ugly.
The only solution I found is to change UIBarButtonItem's tintColor in #IBAction touch event. But I have many UIBarButtonItems and its pretty annoying to write a plenty of code in every #IBAction.
Do you know how to change it globally or, at least, subclass UIBarButtonItem to define it once and use this class everywhere?
I use swift 3, deployment target is iOS 9+
You have your didFinishLaunchingWithOptions function in your AppDelegate witch tells the delegate that the launch process is almost done and the app is almost ready to run.
And in there you can use the appearance on your UIBarButtonItem and UINavigationBar.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Text
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: .selected)
// Images
UINavigationBar.appearance().tintColor = UIColor.orange
return true
}
Result:
You can use appearance to change color globally. Add next lines in didFinishLaunchingWithOptions method
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .selected)
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName: UIColor.white], for: .highlighted)
Swift 4.2
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.darkGray], for: .selected)

Change font in navigation bar in Swift

I'd like to change the font in the navigation bar. However the following code doesn't work, it causes the app to crash.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Lato-Light.ttf", size: 34)!]
return true
}
I am getting the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value(lldb)
I have indeed added the font Lato-Light.ttf to my project so it should be able to find it.
UIFont() is a failable initalizer, it may fail due to several reasons. A forced unwrap using ! crashes your app.
Better initialize it separately and check for success:
if let font = UIFont(name: "Lato-Light.ttf", size: 34) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}
And check if your font file is included in the bundle resources.
Common Mistakes With Adding Custom Fonts to Your iOS App
import UIKit
class TestTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
func configureView() {
// Change the font and size of nav bar text
if let navBarFont = UIFont(name: "HelveticaNeue-Thin", size: 20.0) {
let navBarAttributesDictionary: [NSObject: AnyObject]? = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: navBarFont
]
navigationController?.navigationBar.titleTextAttributes = navBarAttributesDictionary
}
}
}

Resources