复制:

  • 创建一个空白的单 View 项目
  • 将TextField拖到 Canvas 上
  • 将TextField键盘外观设置为Dark
  • 在iPad(设备或模拟器)上运行应用程序
  • 触摸TextField以调出键盘(很暗)
  • 按Home,然后重新进入应用程序
  • 注意键盘将颜色更改为(白色)。

  • 大概键盘颜色会更改为与背景匹配。但是,在这种情况下,某些键仍然不显示,因此这似乎是iOS中的错误(请参见随附的屏幕截图)。

    任何人都想对此有所了解吗?我们正在使用一种解决方法,其中涉及隐藏键盘并重新显示它,但这不是理想的选择。

    最佳答案

    按下主屏幕按钮时,此代码可以关闭键盘,并在应用重新启动时将其带回。您需要将UITextFields 委托(delegate)设置为 View Controller :

    class ViewController: UIViewController, UITextFieldDelegate {
    
    private var _textField: UITextField!
    private var _isFirstResponder: Bool!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "didBecomeActiveNotification:", name: UIApplicationDidBecomeActiveNotification, object: nil)
    
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "willResignActiveNotification:", name: UIApplicationWillResignActiveNotification, object: nil)
    
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func didBecomeActiveNotification(nofication: NSNotification) {
        if _isFirstResponder? == true {
            _textField?.becomeFirstResponder()
        }
    }
    
    func willResignActiveNotification(nofication: NSNotification) {
        if _textField?.isFirstResponder() == true {
            _isFirstResponder = true
            _textField?.resignFirstResponder()
        } else {
            _isFirstResponder = false
        }
    }
    
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        _textField = textField
        return true
    }
    
    }
    

    10-06 13:09
    查看更多