Swift 4中调用SVProgressHUD.show()后加载界面不显示求助
Hey there! Let's figure out why your SVProgressHUD isn't showing up—this is super common for folks new to Swift and CocoaPods, so don't stress 😊
Here are the most likely fixes to try, step by step:
1. Double-Check CocoaPods Setup
First, confirm you've done the basics right:
- At the top of your view controller file, make sure you've imported the pod:
import SVProgressHUD - You must open your project's .xcworkspace file (not the .xcodeproj) in Xcode—this is a super easy mistake to make after running
pod install. - Verify that
pod installcompleted without errors, and SVProgressHUD appears in your Pods directory.
2. Always Call HUD Methods on the Main Thread
SVProgressHUD is a UI component, so all its methods need to run on the main thread. If you're calling show() inside a background task (like a network request or data processing), wrap it in a main queue dispatch:
DispatchQueue.main.async { SVProgressHUD.show() }
This is one of the most frequent causes of UI elements not showing up for new Swift developers.
3. Make Sure You're Not Dismissing It Immediately
If you accidentally call SVProgressHUD.dismiss() right after show() (maybe in a completion handler that fires too fast), the HUD will flash and disappear before you can see it. Test by calling only show() first, without any dismiss code, to see if it appears.
4. Explicitly Set the Container View (For iOS 13+ Scene Setups)
In iOS 13 and later, apps use scene delegates, which can sometimes cause SVProgressHUD to attach to the wrong window. Try explicitly setting it to your app's main window:
if let mainWindow = UIApplication.shared.windows.first { SVProgressHUD.setContainerView(mainWindow) SVProgressHUD.show() }
5. Rule Out Conflicting UI Overlays
If you have other modals, alerts, or full-screen views active when you call show(), the HUD might be hidden behind them. Test in a clean, empty view controller with no other UI elements to isolate the issue.
Quick Test to Verify
Add a simple button to your view controller to trigger the HUD manually:
override func viewDidLoad() { super.viewDidLoad() let testButton = UIButton(type: .system) testButton.setTitle("Show HUD", for: .normal) testButton.addTarget(self, action: #selector(triggerHUD), for: .touchUpInside) testButton.frame = CGRect(x: 50, y: 200, width: 200, height: 50) view.addSubview(testButton) } @objc func triggerHUD() { DispatchQueue.main.async { SVProgressHUD.show() // Auto-dismiss after 3 seconds to test visibility SVProgressHUD.dismiss(withDelay: 3) } }
If this works, the problem was probably related to where or when you were calling show() originally.
内容的提问来源于stack exchange,提问作者Shiva Rama

