如何在SKScene中分配UITextfield位置?

时间:2017-12-11 04:04:25

标签: ios swift sprite-kit uitextfield

当我在CGRect中分配SKScene参数时,它会消失:

import Foundation
import SpriteKit
import UIKit

var username = defaults.string(forKey: "usernameSaved")

class SetupScene: SKScene{

var myTextField: UITextField = UITextField();

override func didMove(to view: SKView) {
    super.didMove(to: view)


    let background = SKSpriteNode(imageNamed: "pixelBackground")
    background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
    background.size = self.size
    background.zPosition = -10
    self.addChild(background)

    self.view?.addSubview(myTextField)
    myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 230.00, height: 33.00));
    myTextField.backgroundColor = UIColor.white
    myTextField.placeholder = "Enter Name Here:"
    myTextField.borderStyle = UITextBorderStyle.line
    myTextField.textAlignment = .center
    myTextField.font = UIFont(name: "04b_25", size: 25)
    myTextField.translatesAutoresizingMaskIntoConstraints = false
    myTextField.center = (self.view?.center)!


}

但是,当我在SKScene之前设置参数时,如下所示,它会显示在屏幕的左上角。

import Foundation
import SpriteKit
import UIKit

var username = defaults.string(forKey: "usernameSaved")

class SetupScene: SKScene{

var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 230.00, height: 33.00));



override func didMove(to view: SKView) {
    super.didMove(to: view)


    let background = SKSpriteNode(imageNamed: "pixelBackground")
    background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
    background.size = self.size
    background.zPosition = -10
    self.addChild(background)

    self.view?.addSubview(myTextField)

    myTextField.backgroundColor = UIColor.white
    myTextField.placeholder = "Enter Name Here:"
    myTextField.borderStyle = UITextBorderStyle.line
    myTextField.textAlignment = .center
    myTextField.font = UIFont(name: "04b_25", size: 25)
    myTextField.translatesAutoresizingMaskIntoConstraints = false
    myTextField.center = (self.view?.center)!
}

我的目标是能够在SKScene开始后对其进行修改,因为我希望其高度为self.size.height/2。但是,我无法在SKScene之前执行此操作,因为它会给我错误:

  

自我。还没有存在。

但是我无法在场景建立后找到一种方法来编辑它的位置。

0 个答案:

没有答案