Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lagging when push uiviewcontroller

When I use storyboard segue, it is pretty smooth showing another viewcontroller onscreen. However, when I'm not using storyboard, just add a simple line of code with navigationController?.pushViewController(UIViewController(), animated: true), it's a little bit lagging in transition.

Also I read about Delay when pushing view controller (iOS). But even when I'm pushing an brand new viewcontroller (no extra code inside), the transition is still a little bit lagging, any idea?

enter image description here

like image 342
Willjay Avatar asked Jun 22 '18 09:06

Willjay


4 Answers

Swift 5.3, Xcode 12

I was facing this same issue, tried adding to main thread, but apparently the problem was I was pushing the viewController created programatically, and it backgroundColor was nil. Just by setting the color in pushedViewController to something else, solved the issue.

self.view.backgroundColor = yourColor
like image 54
Kedar Sukerkar Avatar answered Oct 22 '22 07:10

Kedar Sukerkar


In app delegate, set your window's background color to red.

window?.backgroundColor = .red

Also in the the pushed view controller, set its view to red.

view.backgroundColor = .red

I experienced the same issue when programmatically embedding my view controller in a UINavigationController.

like image 44
Brian Green Avatar answered Oct 22 '22 07:10

Brian Green


I came across the same issue when I was drawing UI programmatically.

In my case, overriding loadView function solved my problem.

import UIKit

class MyViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "My tasks"
}

override func loadView() {
    // You can load your view here. For simplicity, I just give it a background color
    let v = UIView()
    v.backgroundColor = UIColor.orange

    view = v // This line does the magic
}

}

like image 1
Abu-Bakr Avatar answered Oct 22 '22 08:10

Abu-Bakr


Viewcontroller is expecting to render view so add below view background color or set title

override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationItem.title = "Title"
            self.view.backgroundColor = .red
    
        }
like image 1
user1374 Avatar answered Oct 22 '22 08:10

user1374