Hi. I'm pretty new to Swift and I'm having some trouble displaying UITextField in SpriteKit. Init() in SceneMenu will not display the UITextField, while using function showTextField() with touchesBegan() works. How can I display UITextField without the user clicking the button Show TextField?
GameViewController.swift:
class GameViewController: UIViewController {
override func viewDidLoad() {
let scene = SceneMenu(size: view.frame.size)
scene.scaleMode = .aspectFill
scene.backgroundColor = .white
let view = view as! SKView
view.presentScene(scene)
}
}
SceneMenu.swift:
class SceneMenu: SKScene {
override init(size: CGSize) {
super.init(size: size)
let btnAlert = SKLabelNode(text: "Show TextField")
btnAlert.name = "btn_text"
btnAlert.fontSize = 20
btnAlert.fontColor = SKColor.blue
btnAlert.fontName = "Avenir"
btnAlert.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(btnAlert)
let textFieldFrame = CGRect(origin: CGPoint(x: 100, y: 300), size: CGSize(width: 200, height: 30))
let textField = UITextField(frame: textFieldFrame)
textField.backgroundColor = SKColor.blue
textField.placeholder = "Type name"
view?.addSubview(textField)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func showTextField() {
let textFieldFrame = CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: 200, height: 30))
let textField = UITextField(frame: textFieldFrame)
textField.backgroundColor = SKColor.blue
textField.placeholder = "Type name"
view?.addSubview(textField)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let action = atPoint(location)
if action.name == "btn_text" {
showTextField()
}
}
}
}
Post
Replies
Boosts
Views
Activity
I'm pretty new to Swift and I'm having some trouble implementing alerts into my SpriteKit game. Well, it works however it keeps giving warnings:
2022-06-03 02:04:04.465264+0100 Project14[57880:10648157] [LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of a UICollectionViewCell that is managed by a UICollectionView is not supported, and will result in incorrect self-sizing. View: <_UIAlertControllerTextFieldViewCollectionCell: 0x13ed18d70; frame = (0 24; 270 24); gestureRecognizers = <NSArray: 0x6000009c81b0>; layer = <CALayer: 0x600000713600>>
2022-06-03 02:06:16.239075+0100 Project14[57880:10648157] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:], failed to fetch device property for senderID (778835616971358211) use primary keyboard info instead.
How do I get rid of this warnings? Thanks.
let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertController.Style.alert)
alert.addTextField { field in
field.placeholder = "Input your name"
field.isSecureTextEntry = false
field.textAlignment = .left
}
alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in
print("ok")
})
alert.addAction(UIAlertAction(title: "Cancel", style: .default) { _ in
print("cancel")
})
view?.window?.rootViewController?.present(alert, animated: true)
}
I cannot use just present(alert, animated: true). In this case I get compiler error - "Value of type 'SceneMenu' has no member 'present'"