Swift 5 Xcode 10.3自定义Alert背景透明设置问题求助
Hey there! Let's figure out why your dimming background isn't working for your custom alert and fix it up properly.
The core issue here is that system UIAlertController manages its own background styling internally—any direct changes you make to its view's background color will get overwritten by the system during subsequent layout cycles, which explains why your background turned pure black after a few seconds or didn't work at all. Let's build a fully controllable custom alert instead:
Step 1: Create a full-screen dimming background container
First, make a view that covers the entire screen and set its semi-transparent dark background:
// Create the full-screen dim background let dimBackgroundView = UIView(frame: UIScreen.main.bounds) dimBackgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.35) dimBackgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // Add it to your current view controller's view self.view.addSubview(dimBackgroundView)
Step 2: Add your fixed-size content view
Place your pre-made content UIView right in the center of the dim background:
// Replace with your own custom content view (already sized) let customContentView = YourCustomAlertContentView() customContentView.frame = CGRect(x: 0, y: 0, width: 320, height: 240) // Use your desired fixed size // Center the content view customContentView.center = dimBackgroundView.center customContentView.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin] dimBackgroundView.addSubview(customContentView)
Step 3: Add smooth show/hide animations (optional but recommended)
Make the alert feel more natural with a fade-in animation:
// Start with the background hidden dimBackgroundView.alpha = 0 UIView.animate(withDuration: 0.3) { dimBackgroundView.alpha = 1 } // For dismissing the alert later UIView.animate(withDuration: 0.3, animations: { dimBackgroundView.alpha = 0 }) { _ in dimBackgroundView.removeFromSuperview() }
Why your original methods failed
- Storyboard setup:
UIAlertControlleruses internal view layers to handle its background. When you set its view's background color directly, the system resets this property during its own layout updates, causing the pure black background after a delay. - Code-based background color: Same issue—
UIAlertController's top-level view isn't the actual background handler. The system has dedicated internal views for this, so your changes get overwritten instantly.
Extra: Add tap-to-dismiss functionality
If you want users to close the alert by tapping the dimmed area, add a gesture recognizer:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissCustomAlert)) dimBackgroundView.addGestureRecognizer(tapGesture) // Make sure your content view doesn't block the tap customContentView.isUserInteractionEnabled = true
Then implement the dismiss method:
@objc func dismissCustomAlert() { UIView.animate(withDuration: 0.3, animations: { self.dimBackgroundView.alpha = 0 }) { _ in self.dimBackgroundView.removeFromSuperview() } }
This approach gives you full control over the dimming effect without any system interference!
内容的提问来源于stack exchange,提问作者Ashfaque

