The code below is used to push another view controller onto the navigation stack.
When using instantiateViewControllerWithIdentifier
, the segue is noticeably sluggish the first time (~3 seconds) but occurs reasonably fast each subsequent time. Other SO posts suggested ensuring the segue occurs on the main thread, which the code accomplishes, but this didn't fix the problem.
However, using performSegueWithIdentifier
causes no delay.
The viewDidLoad
code for SendViewController
is the same for the first and subsequent pushes.
Tried blanking out viewDidLoad
for the destination view controller, but still the lag exists for instantiateViewControllerWithIdentifier
but not for performSegueWithIdentifier
.
How to fix the delay with instantiateViewControllerWithIdentifier
?
No delay:
@IBAction func buttonTapped(sender: UIButton) {
performSegueWithIdentifier(SendSegue, sender: self)
}
Results in delay when showing SendViewController for first time:
@IBAction func buttonTapped(sender: UIButton) {
dispatch_async(dispatch_get_main_queue()) {
let vc = self.storyboard!.instantiateViewControllerWithIdentifier(self.SendViewControllerID) as! SendViewController
self.navigationController!.pushViewController(vc, animated: true)
}
}
The problem was isolated to the presence of a UITextField in the destination view controller, that is, removing the UITextField removes the lag.
Then it was further isolated to the presence of a custom font.
In other words, using the system font on the UITextField, rather than a custom font, removes the lag. No explanation why, but it works.