Skip to content

Instantly share code, notes, and snippets.

@samithaf
Created June 7, 2019 00:08
Show Gist options
  • Save samithaf/83efbc7b6d86448d0e4e426c68b29985 to your computer and use it in GitHub Desktop.
Save samithaf/83efbc7b6d86448d0e4e426c68b29985 to your computer and use it in GitHub Desktop.
MBProgressHUD for ReactNative

#How to use

  • Add pod 'MBProgressHUD', '~> 1.1.0' to your pod file and run pod install
  • Copy ProgressIndicator.swift and ProgressIndicator.m files to your xcode project.
  • If your project based on Swift you may need to add #import "MBProgressHUD.h" to the BridgingHeader file
  • See the sample use in hello.screen.js
import {
NativeModules,
} from 'react-native';
useLayoutEffect(() => {
if (isLoading) {
NativeModules.ProgressIndicator.show('Signing in');
return;
}
NativeModules.ProgressIndicator.hide();
}, [isLoading]);
#import "React/RCTBridgeModule.h"
@interface RCT_EXTERN_MODULE(ProgressIndicator, NSObject)
RCT_EXTERN_METHOD(show:(NSString *)text)
RCT_EXTERN_METHOD(hide)
@end
import Foundation
@objc(ProgressIndicator)
class ProgressIndicator: NSObject {
@objc
static func requiresMainQueueSetup() -> Bool {
return true
}
@objc
func show(_ text: String) {
DispatchQueue.main.async {
if let topVC = self.getTopViewController() {
let hud = MBProgressHUD.showAdded(to: topVC.view, animated: true)
hud.label.text = text
hud.isUserInteractionEnabled = true
}
}
}
@objc
func hide() {
DispatchQueue.main.async {
if let topVC = self.getTopViewController() {
MBProgressHUD.hide(for: topVC.view, animated: true)
}
}
}
func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return getTopViewController(base: nav.visibleViewController)
} else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return getTopViewController(base: selected)
} else if let presented = base?.presentedViewController {
return getTopViewController(base: presented)
}
return base
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment