为什么焦点上的 searchBar 会稍微改变文本的颜色?
Why does the searchBar on focus slightly change the color of the text?
编辑 2
经过更仔细的检查,键盘似乎与此问题直接相关。
searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.dark
键盘的外观似乎会影响搜索栏文本的颜色。我不知道为什么会这样。键盘的默认外观是黑色的,因此当键盘可见时,searchBar 文本变为黑色。关闭键盘后,searchBar 文本会恢复为我指定的原始颜色(白色)。
编辑
仔细检查后,问题似乎与键盘直接相关。只要键盘处于活动状态(即可见且未关闭),文本的颜色就会不同。
键盘关闭的那一刻,颜色发生变化。
假设 "focus" 不是问题所在。
此外searchController.searchBar.setTextColor(color: .white)
和"noticed"色调是两种完全不同的东西。他们根本没有链接。
描述
我已将 searchBar
的文本颜色设置为 white
,但是当 searchBar
处于焦点时,颜色有轻微的“色调" 到它。
我将 tint 放在引号中是因为我实际上试图将 searchBar 的 tint 颜色设置为白色,但没有成功。
代码:
这是我用来创建 SearchController
的代码。此代码是在我的 AppDelegate
中调用并添加到我的 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
截图:
上图显示搜索栏处于焦点状态,文本带有淡淡的绿色。
上图显示搜索栏未聚焦且文本为白色。
问题:
为什么焦点上的 searchBar 会稍微改变文本的颜色?
当键盘处于活动状态时,键盘外观会影响 searchBar 文本。向 searchBar 添加自定义外观不是一个好主意。最好的解决方法是使用默认外观。
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"))
十六进制“#c5c5c5
”对应于键盘深色外观的按键颜色。这样,当键盘关闭或激活时,searchBar 文本的颜色不会发生变化。
编辑 2
经过更仔细的检查,键盘似乎与此问题直接相关。
searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.dark
键盘的外观似乎会影响搜索栏文本的颜色。我不知道为什么会这样。键盘的默认外观是黑色的,因此当键盘可见时,searchBar 文本变为黑色。关闭键盘后,searchBar 文本会恢复为我指定的原始颜色(白色)。
编辑
仔细检查后,问题似乎与键盘直接相关。只要键盘处于活动状态(即可见且未关闭),文本的颜色就会不同。
键盘关闭的那一刻,颜色发生变化。
假设 "focus" 不是问题所在。
此外searchController.searchBar.setTextColor(color: .white)
和"noticed"色调是两种完全不同的东西。他们根本没有链接。
描述
我已将 searchBar
的文本颜色设置为 white
,但是当 searchBar
处于焦点时,颜色有轻微的“色调" 到它。
我将 tint 放在引号中是因为我实际上试图将 searchBar 的 tint 颜色设置为白色,但没有成功。
代码:
这是我用来创建 SearchController
的代码。此代码是在我的 AppDelegate
中调用并添加到我的 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
截图:
上图显示搜索栏处于焦点状态,文本带有淡淡的绿色。
上图显示搜索栏未聚焦且文本为白色。
问题:
为什么焦点上的 searchBar 会稍微改变文本的颜色?
当键盘处于活动状态时,键盘外观会影响 searchBar 文本。向 searchBar 添加自定义外观不是一个好主意。最好的解决方法是使用默认外观。
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"))
十六进制“#c5c5c5
”对应于键盘深色外观的按键颜色。这样,当键盘关闭或激活时,searchBar 文本的颜色不会发生变化。