Skip to main content

Custom Loader (Activity Indicator) in Swift

Creating a custom activity indicator in Swift allows you to tailor the appearance and behavior of your loading spinner to fit the style of your app. Here's a step-by-step guide to creating a simple custom activity indicator using UIView Step 1: Create a New Swift File for the Custom Activity Indicator Create a new Swift file and name it  RotatingCirclesView.swift . Add the following code to define a custom UIView subclass for your activity indicator: // //   RotatingCirclesView.swift //   Welcome In // //   Created by Praveen Kumar on 05/09/24. // import UIKit class RotatingCirclesView : UIView {          let circle1 = UIView ( frame : CGRect ( x : 20 , y : 20 , width : 60 , height : 60 ))     let circle2 = UIView ( frame : CGRect ( x : 120 , y : 20 , width : 60 , height : 60 ))          let position : [ CGRect ] = [ CGRect ( x : 30 , y : 20 , width : 60 , height : 60 ), CGRect ( x : 60 , y : 15 , width : 70 , height : 70 ), CGRect ( x : 110 , y : 20 , width : 60 , heigh

Add a Scene Delegate to your existing project with Storyboard in Swift

To add a scene delegate, first, create a new Swift file that you’ll call "SceneDelegate" containing a subclass of UIResponder, just like the AppDelegate, and that conforms to UIWindowSceneDelegate. 

As your app might supports other versions than iOS 13, make this class only available for iOS 13. This is what you should have :

If you are working a project that is storyboard based,

please set storyboard initial view controller

SceneDelegate.swift

import UIKit



@available(iOS 13.0, *)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {


    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController")


        let mainNavigationController = UINavigationController(rootViewController: initialViewController)

                self.window?.rootViewController = mainNavigationController

        

                 guard let _ = (scene as? UIWindowScene) else { return }

        

                if let windowScene = scene as? UIWindowScene {

                    self.window = UIWindow(windowScene: windowScene)

                    

                    self.window!.rootViewController = mainNavigationController

                    self.window!.makeKeyAndVisible()

                }

    }


    func sceneDidDisconnect(_ scene: UIScene) {

        // Called as the scene is being released by the system.

        // This occurs shortly after the scene enters the background, or when its session is discarded.

        // Release any resources associated with this scene that can be re-created the next time the scene connects.

        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).

    }


    func sceneDidBecomeActive(_ scene: UIScene) {

        // Called when the scene has moved from an inactive state to an active state.

        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.

    }


    func sceneWillResignActive(_ scene: UIScene) {

        // Called when the scene will move from an active state to an inactive state.

        // This may occur due to temporary interruptions (ex. an incoming phone call).

    }


    func sceneWillEnterForeground(_ scene: UIScene) {

        // Called as the scene transitions from the background to the foreground.

        // Use this method to undo the changes made on entering the background.

    }


    func sceneDidEnterBackground(_ scene: UIScene) {

        // Called as the scene transitions from the foreground to the background.

        // Use this method to save data, release shared resources, and store enough scene-specific state information

        // to restore the scene back to its current state.

    }

    

}


 AppDelegate.swift


import UIKit



@UIApplicationMain

@available(iOS 13.0, *)


class AppDelegate: UIResponder, UIApplicationDelegate {


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        return true

    }


    // MARK: UISceneSession Lifecycle


    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {

        // Called when a new scene session is being created.

        // Use this method to select a configuration to create the new scene with.

        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)

    }


    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {

        // Called when the user discards a scene session.

        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.

        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.

    }



}


Just go in your info.plist file and add these lines :

<key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <true/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>

                    <key>Storyboard Name</key>
                    <string>Main</string>
                </dict>
            </array>
        </dict>
    </dict>

Comments

Post a Comment