Since Xcode 12 uitextfields of existing project has black background

1.2k views Asked by At

A few days ago I upgraded Xcode 11 to Xcode 12, since than my app doesn't build correctly. One of the problems is that my white uitextfields become black! In the image below it should show 4 uitextfields inside the red rectangle as a stack, but now you see nothing, because they turned black (The are not disappeared)! I haven't changed anything, just upgraded Xcode to release version 12. I tested on iOS 12.x, iOS 13.x and iOS 14, on all versions uitextfields turn black now, just because I use Xcode 12.

enter image description here

All my uitextfields turned black. My app doesn't support dark mode, but I had no problems in Xcode 11.4.

I also tried to fix the problem with the following code:

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

But with no success :(

The only solution is to manually set all the uitextfields' background to white, but that's frustrating. Do you know what causes the uitextfields to be black instead of default white and how I can fix it from central place?

3

There are 3 answers

1
Jorge Luis Peña Lopez On

I had a similar problem in an application and according to Apple documentation you can try add same "ligth" style for all your screens added the next line to your Info.plist file of your application

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>

Setting this key to Light causes the system to ignore the user's preference and always apply a light appearance to your app.

Important

Supporting Dark Mode is strongly encouraged. Use the UIUserInterfaceStyle key to opt out only temporarily while you work on improvements to your app's Dark Mode support.

Reference: https://developer.apple.com/documentation/xcode/supporting_dark_mode_in_your_interface/choosing_a_specific_interface_style_for_your_ios_app

1
Jam On

I use xib in Xcode 12. UITextField backgroundColor default is clear color. You just need to change your textField backgroundColor.

4
Fabio On

Try to present your first controller and override window user interface in scene delegate like this:

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
    }
}

if you don't solve the issue and set manually bg color I suggest to write an extension that set bg to white like this:

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])
 }
}

how to use:

let yourTextField = UITextField()
myTextField(tf: yourTextField, placeHolder: "Your placeHolder...", placholderColor: .yourColor, textColor: .yourColor)

if you want tf padding add this extension:

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
 }
}

after that add this line to myTextField func:

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