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
}
}
}
Related
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
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 */
}
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
I am getting issue with iPhoneX back button image. It is not in right position as you can see in the screenshot
I am using below code for setting back image
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().backIndicatorImage = #imageLiteral(resourceName: "btn-back")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "btn-back")
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), for: .default)
return true
}
}
Can anyone please explain why it's not at correct position and how to fix this ?
You can use topItem?.title for setting up Navigation title as:
Method 1:
self.navigationController?.navigationBar.topItem?.title = ""
Output:
Method 2:
let yourBackImage = UIImage(named: "back-Image")
self.navigationController?.navigationBar.tintColor = .red//.blue as you required
self.navigationController?.navigationBar.backIndicatorImage = yourBackImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
self.navigationController?.navigationBar.topItem?.title = ""
Output:
Note: As Apple Human Interface GuideLine The navigation bar back image size should be-
75px × 75px(3x)
Sample image 3x(75 x 75)-
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?