swift - UITextField 在 SKScene 上不可见

标签 swift uitextfield skscene addsubview

我尝试将 UITextField 添加到 SKScene。我正在使用 Swift 3 和 Xcode 8。我尝试了以下操作:

import Foundation
import SpriteKit

class MyScene: SKScene {

    init(size: CGSize, score: Highscore, optionen: GameOptions) {
        super.init(size: size)

        let stepsTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
        stepsTextField.placeholder = "Enter text here"
        stepsTextField.font = UIFont.systemFont(ofSize: 15)
        stepsTextField.borderStyle = UITextBorderStyle.roundedRect
        stepsTextField.autocorrectionType = UITextAutocorrectionType.no
        stepsTextField.keyboardType = UIKeyboardType.default
        stepsTextField.returnKeyType = UIReturnKeyType.done
        stepsTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
        stepsTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
        self.view?.addSubview(stepsTextField)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

但我得到的只是黑屏。我错过了什么?

感谢您的帮助!

最佳答案

需要将其添加到didMove(to view: SKView)

通过将代码添加到以下函数,您可以看到您的文本字段。

    override func didMove(to view: SKView) {
    let stepsTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
    stepsTextField.placeholder = "Enter text here"
    stepsTextField.font = UIFont.systemFont(ofSize: 15)
    stepsTextField.borderStyle = UITextBorderStyle.roundedRect
    stepsTextField.autocorrectionType = UITextAutocorrectionType.no
    stepsTextField.keyboardType = UIKeyboardType.default
    stepsTextField.returnKeyType = UIReturnKeyType.done
    stepsTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
    stepsTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
    self.view?.addSubview(stepsTextField)
}

关于swift - UITextField 在 SKScene 上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45304979/

相关文章:

ios - SpriteKit 场景没有重置?

ios - 在 Argo 中解码泛型

ios - JSON 到自定义对象数组 (Swift)

ios - 如何引用 iOS UITextFields

ios - 如何防止用户输入密码时文本移动?

objective-c - UITableView 滚动后 UITextFields 重叠

swift - SKEmitterNode 对另一个对象的强制爆炸

ios - 允许 SpriteKit 中的触摸传播

ios - 是否有 [NSString stringWithFormat :] for NSAttributedString 的任何类似物

ios - 使用计算属性 setter 导致 "Cannot assign to the result of this expression"