PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 29, 2022

[FIXED] Why does the searchBar on focus slightly change the color of the text?

 September 29, 2022     swift, swift3, tvos, uisearchbar     No comments   

Issue

Edit 2

Upon even closer inspection, it would appear that the keyboard is directly related to this issue.

searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.dark

The Appearance of the keyboard seems to influence the color of the searchBar text. I have no idea why that occurs. The default appearance of the keyboard is black, so when the keyboard is visible, the searchBar text turns black. When the keyboard is dismissed, the searchBar text goes back to the original color I specified (white).

Edit

After closer inspection, the problem seems to be directly linked to the keyboard. As long as the keyboard is active (meaning visible and not dismissed) then the color of the text is different.

The moment the keyboard gets dismissed, the color changes.

Would presume that "focus" isn't the problem.

Also searchController.searchBar.setTextColor(color: .white) and the "noticed" tint color are two completely different things. They are not linked at all.

Description

I've set the text color of my searchBar to white, however when the searchBar is on focus the color has a slight "tint" to it.

I've put tint in quotes because I actually attempted to set the searchBar's tint color to white, without any luck.

Code:

Here is the code I use to create the SearchController. This code is part of a function that is called in my AppDelegate and added to my TabBarController.

        //... more code above
        //Inside function called "configureSearchController" within AppDelegate
        let searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.searchBar.placeholder = NSLocalizedString("Enter keyword (e.g. Gastric Bypass)", comment: "")
        searchController.view.backgroundColor = Constants.Color.backgroundColorDark
        searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.dark
        searchController.searchBar.backgroundColor = Constants.Color.backgroundSearchBarColor

        //Here is where I set the color of the search bar text.
        searchController.searchBar.setTextColor(color: .white)

        //Here is where I attempt to remove any possible color changes
        //or effect... none of the following 2 lines work
        searchController.searchBar.tintColor = .white
        searchController.searchBar.barTintColor = .white

        searchController.hidesNavigationBarDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = true
        searchController.searchBar.searchBarStyle = .minimal
        searchController.searchBar.sizeToFit()

        //... more code below

Screenshots:

image on focus

Image above shows the searchBar in focus and the text with a slight green tint.

image not on focus

Image above shows the searchBar not in focus and the text white.

Question:

Why does the searchBar on focus slightly change the color of the text?


Solution

The keyboard appearance influences the searchBar text when the keyboard is active. Adding custom appearance to the searchBar is not a good idea. The best workaround was to use the default appearances.

extension UIColor {
    convenience init(hexString: String) {
        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int = UInt32()
        Scanner(string: hex).scanHexInt32(&int)
        let a, r, g, b: UInt32
        switch hex.characters.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (1, 1, 1, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
    }
}

searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.dark
searchController.searchBar.setTextColor(color: UIColor.init(hexString: "#c5c5c5"))

The hex "#c5c5c5" corresponds to the key color from the keyboard dark appearance. This way there is no change in color in the searchBar text, when the keyboard gets dismissed or becomes active.



Answered By - kemicofa ghost
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing