Blaluma
Blaluma

Reputation: 26

Text Field only gets visible after another action

I have created a UITextField which contains UIPickerView and UIToolbar to have a nice selection wheel with a "Save" and a "Cancel" button. pickerTextField is defined at the top of my class:

let pickerTextField = UITextField()

The rest is called when a user taps on a SKSpriteNode so I'm tracking the touch by overwriting the touchesBegan function

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent?) {
    if let touch = touches.first as? UITouch {
        var touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)
        if let name = touchedNode.name
        {
            if name == "overlay_player"
            {
                pickerTextField.frame = CGRectMake(0, 0, view!.bounds.width, view!.bounds.height*0.5)

                let playerCountryPicker = UIPickerView()
                playerCountryPicker.delegate = self
                playerCountryPicker.dataSource = self
                playerCountryPicker.layer.borderColor = UIColor.blackColor().CGColor
                playerCountryPicker.layer.borderWidth = 1
                playerCountryPicker.backgroundColor = UIColor.whiteColor()
                playerCountryPicker.showsSelectionIndicator = true

                var toolBar = UIToolbar()
                toolBar.barStyle = UIBarStyle.Default
                toolBar.translucent = true
                toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
                toolBar.sizeToFit()

                var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "savePickerPlayer:")
                var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
                var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPickerPlayer:")
                toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
                toolBar.userInteractionEnabled = true

                pickerTextField.inputView = playerCountryPicker
                pickerTextField.inputAccessoryView = toolBar

                self.view?.addSubview(pickerTextField)

            }
        }
    }

}

My issue is: When tapping on the SKSpriteNode the code is executed but the text field won't show. But if I tap on any other object (e.g. a second SKSpriteNode) the text field shows up, but without executing the code once more.

From my perspective it seems the view just isn't updated but trying it with

self.view?.setNeedsDisplay()
self.view?.setNeedsLayout()
self.view?.reloadInputViews()

has no effect whatsoever. I assume I'm missing a simple point here but I'm struggling to find it. Any ideas?

/Edit: Found a work-around I can propose to persons with the same problem (not a solution thought): Replace the UITextField with an UILabel. You need to set yourLabel.userInteractionEnabled = true and instead using UITextField.inputView and UITextField.inputAccessoryView you'll just add the UIToolbar and the UIPickerView as subviews.

You'll have to handle the animations yourself but a simple

class func animateWithDuration(_ duration: NSTimeInterval,
                    animations animations: () -> Void)

should do the trick. General question why two actions are needed to show the UITextField is still open.

Upvotes: 0

Views: 59

Answers (1)

Russell
Russell

Reputation: 3089

It could be that the pickerTextField subview is not at the front.

Try adding the following.

self.view?.addSubview(pickerTextField)
self.view?.bringSubviewToFront(pickerTextField)

Upvotes: 1

Related Questions