Customize every single UISearchBar From AppDelegate Not Working - ios

I have this code below in my AppDelegate.swift:
func customizeBars() {
let bartintColor = UIColor(colorLiteralRed: 20/255, green: 160/255, blue: 160/255, alpha: 1)
UISearchBar.appearance().tintColor = bartintColor
window!.tintColor = UIColor(red: 10/255, green: 80/255, blue: 80/255, alpha: 1)
}
I call this function (customizeBars()) in my didFinishLaunchingWithOptions method:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
customizeBars()
return true
}
I also have this in the UISearchBArDelegate if it makes a difference:
func position(for bar: UIBarPositioning) -> UIBarPosition {
return UIBarPosition.topAttached
}
But for some reason when I run my app it doesn't change the tintColor of any of the UISearchBars I have in my app or my window tint color. Is there something I am doing wrong?

You can change the search bar color using this below line of code -
UISearchBar.appearance().barTintColor = UIColor.red // Change the color here
But I saw that, tint color(for cancel button, search icon and text color) is not working in iOS 10, swift 3.
It used to work before in older versions. Let me try if i can find something to change the tint color of search bar.

Related

barTintColor not applied when NavigationBar is Large Titles

I'm updating an app that was compiled on Xcode 10 and running fine up through iOS 13. I wanted to make some changes so recompiled on Xcode 11 and now have a problem with the barTintColor.
If 'Large Titles' is set to 'Always' my custom barTintColor is not applied -- I just get the default gray. If 'Large Titles' is set to 'Never', my custom barTintColor is applied as expected. If 'Large Titles' is set to 'Automatic', the NavBar is default gray when Large Titles are shown and my custom color when small titles are shown. For example, when the TableView below my nav bar is pushed up, the default large title switches to small title, and my NavBar changes color. Normal behavior would be for it always to be my custom color.
The relevant code from my ViewController class, with the last line being the one that sets the barTintColor:
override func viewDidLoad() {
super.viewDidLoad()
setDelegates()
setTableViewHeightForCollapsingHeaders()
setNavigtionBarItems()
doSplitViewManagement()
}
override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
super.viewWillAppear(animated)
updateUI()
}
fileprivate func setNavigtionBarItems() {
//set up UI buttons
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
navigationController?.navigationBar.barTintColor = UIColor(hex: 0x5da0a2)
}
Any idea why the behavior changed, and how to fix it?
Apple's documentation that glotcha pointed to was critical to solving the problem although there was a bit more to it. Here's the updated version of my setNavigationBarItems() that works in iOS 13:
fileprivate func setNavigtionBarItems() {
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = myBackgroundColor
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
//navigationController?.navigationBar.compactAppearance = appearance
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = myBackgroundColor
}
}
A key point in my case was that my Navigation Bar is set (in Autolayout) with 'Large Titles' as 'Automatic'. This makes it necessary to include the .scrollEdgeAppearance line so that the custom appearance applies when it transitions from large to compact. It turned out the .compactAppearance line was not required because I use the same color for both. If I wanted different appearance settings for large and compact then a line with .compactAppearance would also be useful.
There's a new API since iOS 13 https://developer.apple.com/documentation/uikit/uinavigationbarappearance
The backgroundColor property you're looking for is in the super class
https://developer.apple.com/documentation/uikit/uibarappearance
Some extra sample code here
https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar
If you want a single place to do it for the whole app:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let barBackgroundColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0)
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = barBackgroundColor
appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
return true
}

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

How to change tintColor of UIBarButtonItem in Swift?

I want to change the color of my right bar button item from black to white. It is a button as a search icon. I have not coded the search implementation yet as I want to get the main interface completed first. I thought that I'd written the correct codes so it should appear as white, but it seems to still appear as black in both the storyboard and simulator.
In the storyboard, I have also set it to white.
Here is my code, which is located in the AppDelegate.swift file:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Changing the status bar's colour to white
UIApplication.sharedApplication().statusBarStyle = .LightContent
// Changing the navigation controller's background colour
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 165.0/255.0, blue: 227.0/255.0, alpha: 1.0)
// Changing the navigation controller's title colour
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
// Changing the colour of the bar button items
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// Changing the tint colour of the tab bar icons
UITabBar.appearance().tintColor = UIColor(red: 0.0/255/0, green: 165.0/255.0, blue: 227.0/255.0, alpha: 1.0)
return true
}
Here is an image of the simulator:
I find it odd that this line of code doesn't work. Any solution?
SWIFT 4
After you declare your custom barbuttonitem as an outlet:
#IBOutlet weak var yourBarButtonItem: UIBarButtonItem!
You can define your color however you like.
yourBarButtonItem.tintColor = .red
The problem was that the button was automatically set as custom. I refigured it to system.
Setting Text Color For Normal Text:
let uiBarButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logOutTapped))
uiBarButton.tintColor = UIColor.appColor
navigationItem.rightBarButtonItems = [uiBarButton]
Setting Text Color For Attributed Text:
let uiBarButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logOutTapped))
uiBarButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.appColor], for: .normal)
navigationItem.rightBarButtonItems = [uiBarButton]

Can't read titleTextAttributes on appearance for UITabBarItem

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?

Resources