Search code examples
swiftxcodedebugginguikitlldb

How to use LLDB (XCode) to change the view background color at runtime and update view UI on the simulator


I have this code:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Line where the breakpoint will be placed
        view.backgroundColor = UIColor.green
    }
}

I want to add breakpoint, execute the app on the simulator, and when the execution is paused, I want to use the LLDB to change backgroundColor to blue and see the background on the simulator on blue color.

How to do that?


Solution

  • I found this solution:

    • Set a breakpoint in the same line that you want do change/update in runtime
    • Run the app on the simulator. When the execution is paused, type this expression on the main thread in the XCode console:
    e DispatchQueue.main.async { self.view.backgroundColor = UIColor.blue }
    
    • Hit Enter and release the execution. You will see the background changing from green to blue on the simulator.

    This is a good UI solution for UIkit projects without SwiftUI #Preview{}