Ios 由于Xcode 12,现有项目的uitextfields具有黑色背景

Ios 由于Xcode 12,现有项目的uitextfields具有黑色背景,ios,objective-c,uitextfield,xcode12,Ios,Objective C,Uitextfield,Xcode12,几天前,我将Xcode 11升级为Xcode 12,因为我的应用程序无法正确构建。其中一个问题是我的白色uitextfields变为黑色!在下面的图像中,它应该在红色矩形中显示4个uitextfields作为堆栈,但现在您什么也看不到,因为它们变为黑色(这些字段没有消失)!我没有做任何更改,只是将Xcode升级到版本12。我在iOS 12.x、iOS 13.x和iOS 14上进行了测试,所有版本的uitextfields现在都变黑了,因为我使用的是Xcode 12 我所有的田野都变黑了。我的应

几天前,我将Xcode 11升级为Xcode 12,因为我的应用程序无法正确构建。其中一个问题是我的白色uitextfields变为黑色!在下面的图像中,它应该在红色矩形中显示4个uitextfields作为堆栈,但现在您什么也看不到,因为它们变为黑色(这些字段没有消失)!我没有做任何更改,只是将Xcode升级到版本12。我在iOS 12.x、iOS 13.x和iOS 14上进行了测试,所有版本的uitextfields现在都变黑了,因为我使用的是Xcode 12

我所有的田野都变黑了。我的应用程序不支持暗模式,但我在Xcode 11.4中没有问题

我还尝试使用以下代码修复此问题:

if (@available(iOS 13.0, *)) {
    if ([self.window respondsToSelector:NSSelectorFromString(@"overrideUserInterfaceStyle")]) {
        [self.window setValue:@(UIUserInterfaceStyleLight) forKey:@"overrideUserInterfaceStyle"];
    }
}
但没有成功:(


唯一的解决方案是手动将所有uitextfields的背景设置为白色,但这很令人沮丧。您知道是什么原因导致uitextfields变为黑色而不是默认的白色,以及我如何从中央位置修复它吗?

尝试在场景代理中显示您的第一个控制器并覆盖窗口用户界面,如下所示:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(windowScene: windowScene)
    window?.makeKeyAndVisible()
    let controller = YourFirstViewController()
    window?.rootViewController = controller
    if #available(iOS 13.0, *) {
        self.window?.overrideUserInterfaceStyle = .light
    }
}
extension UIViewController {
func myTextField(tf: UITextField, placeHolder: String, placholderColor: UIColor, textColor: UIColor) {
    let textfield = tf
    textfield.backgroundColor = .white
    textfield.font = .systemFont(ofSize: 16, weight: .regular)
    textfield.textColor = textColor
    textfield.layer.cornerRadius = 14
    textfield.clipsToBounds = true
    textfield.attributedPlaceholder = NSAttributedString(string: placeHolder, attributes: [.foregroundColor: placholderColor])
 }
}
如果您没有解决问题并手动设置bg颜色,我建议编写一个扩展,将bg设置为白色,如下所示:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(windowScene: windowScene)
    window?.makeKeyAndVisible()
    let controller = YourFirstViewController()
    window?.rootViewController = controller
    if #available(iOS 13.0, *) {
        self.window?.overrideUserInterfaceStyle = .light
    }
}
extension UIViewController {
func myTextField(tf: UITextField, placeHolder: String, placholderColor: UIColor, textColor: UIColor) {
    let textfield = tf
    textfield.backgroundColor = .white
    textfield.font = .systemFont(ofSize: 16, weight: .regular)
    textfield.textColor = textColor
    textfield.layer.cornerRadius = 14
    textfield.clipsToBounds = true
    textfield.attributedPlaceholder = NSAttributedString(string: placeHolder, attributes: [.foregroundColor: placholderColor])
 }
}
如何使用:

let yourTextField = UITextField()
myTextField(tf: yourTextField, placeHolder: "Your placeHolder...", placholderColor: .yourColor, textColor: .yourColor)
如果要添加此扩展名:

extension UITextField {

 func setPadding(left: CGFloat, right: CGFloat){

 let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: left, height: self.frame.size.height))
 self.leftView = paddingView
 self.leftViewMode = .always

 let paddingViewRight = UIView(frame: CGRect(x: 0, y: 0, width: right, height: self.frame.size.height))
 self.rightView = paddingViewRight
 self.rightViewMode = .always
 }
}
之后,将此行添加到myTextField func:

textfield.setPadding(left: 20, right: 20)

我在一个应用程序中遇到了类似的问题,根据苹果的文档,您可以尝试为所有屏幕添加相同的“LIGHT”样式,并将下一行添加到应用程序的Info.plist文件中

UIUserInterfaceStyle
轻的
将此键设置为“灯光”会导致系统忽略用户的首选项,并始终为应用程序应用灯光外观

重要

强烈建议支持暗模式。在改进应用程序的暗模式支持时,使用UIUserInterfaceStyle键只能暂时退出

参考资料:

我在Xcode 12中使用xib。 UITextField backgroundColor默认值为“透明颜色”。
您只需要更改文本字段背景颜色。

Thnx获取您的答案,但它对我不起作用。您是否在Xcode 12中也遇到了同样的问题?@AndaluZ是的,但我用我的答案解决了…如果您手动设置bg颜色,我将使用扩展更新我的答案,这将使您的代码更干净,工作更轻松…抱歉:我的项目仍然在Objc中,我没有使用UIScene。我认为现在唯一的解决方案是手动将使用的UITEXFED设置为故事板中的白色。我认为这是XCODER 12的一个错误。我赞成你的答案,但不幸的是,我没有找到一个可行的答案。@ Adaluz Thx,对于你的赞成票,我有你同样的怀疑,是一个XCODE 12 bug…哈哈。I’祝你愉快:)仔细检查你的textfields backgroundColor属性,确保它们没有设置为SystemBackground我也这样做了,令人恼火的是,我有16个UITextFields,到处都是。+1是有用的解决方案。我已经这样做了,而且以编程的方式,仔细阅读我的问题。