I had the similar problem but on the actual device.
I talked to apple developer technical support (Apple DTS) and they told me that it's a swift compiler or Xcode bug.
I did a little workaround. Don't think that It's a best solution until apple solves this problem but it works without any problem now.
Just override the UITextField class:
class PBTextField : UITextField {
var keyboardShowDate = NSDate()
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
self.initialisation()
}
override init(frame: CGRect)
{
super.init(frame: frame)
self.initialisation()
}
func initialisation()
{
self.addTarget(self, action: "editingDidBegin:", forControlEvents: UIControlEvents.EditingDidBegin)
self.addTarget(self, action: "editingDidEnd:", forControlEvents: UIControlEvents.EditingDidEnd)
}
// MARK: Delegates
func editingDidBegin(sender: AnyObject)
{
self.becomeFirstResponder()
self.keyboardShowDate = NSDate()
}
func editingDidEnd(sender: AnyObject)
{
if(fabs(self.keyboardShowDate.timeIntervalSinceNow) <= 0.5)
{
self.becomeFirstResponder()
dispatch_after(0.1, block: { () -> Void in
self.becomeFirstResponder()
})
}
}
}
dispatch methods:
typealias dispatch_cancelable_block_t = ((cancel: Bool) -> Void)
func dispatch_async(block: dispatch_block_t, background: Bool = true) -> dispatch_cancelable_block_t?
{
return dispatch_after(0, block: block, background: background)
}
func dispatch_after(delay: NSTimeInterval = 0, block: dispatch_block_t, background: Bool = false) -> dispatch_cancelable_block_t?
{
var cancelableBlock : dispatch_cancelable_block_t?
let delayBlock : dispatch_cancelable_block_t = { (cancel) -> Void in
if(cancel == false)
{
if(background)
{
block()
}
else
{
dispatch_async(dispatch_get_main_queue(), block)
}
}
cancelableBlock = nil
};
cancelableBlock = delayBlock
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * NSTimeInterval(NSEC_PER_SEC))), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
if let cb = cancelableBlock
{
cb(cancel: false)
}
})
return cancelableBlock
}
func cancel_block(block: dispatch_cancelable_block_t)
{
block(cancel: true)
}
Hope this will help to someone.
If you got better solution, please let me know in comments.
Thanks
Giorgi