用户登录后切换ViewController,新控制器视图元素不显示求助
Hey there, let's break down why you're only seeing the background gradient (or a black screen) when navigating to GeneralChooser:
核心问题分析
First off, the most obvious root cause: your GeneralChooser view controller doesn't have any visible UI elements added to it—you're only setting a background gradient layer. Let's unpack the details:
- When you call
ViewHelper.BackGroundColor(view: self.view), you're adding a gradient to the view, but there are no buttons, labels, or other controls on top of it. So all you'll see is the gradient itself. - If you skip calling that background method, the default background color of a
UIViewController's view is black—hence the black screen. Your initial view controller works because you've presumably added UI elements to it already.
There's also a subtle issue with how you're setting the gradient layer's frame, which might cause it to display incorrectly on different screen sizes or when the view resizes.
解决方案
1. Add Visible UI Elements to GeneralChooser
You need to add actual views (like labels, buttons, etc.) to GeneralChooser. Here's an example of adding a welcome label with Auto Layout:
class GeneralChooser: UIViewController{ override func viewDidLoad() { super.viewDidLoad() ViewHelper.BackGroundColor(view: self.view) // Add a sample welcome label let welcomeLabel = UILabel() welcomeLabel.text = "Logged In Successfully!" welcomeLabel.textColor = .white welcomeLabel.font = .systemFont(ofSize: 22, weight: .semibold) welcomeLabel.translatesAutoresizingMaskIntoConstraints = false view.addSubview(welcomeLabel) // Center the label on the screen NSLayoutConstraint.activate([ welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) } }
2. Fix the Gradient Layer's Frame Issue
Right now, you're setting newLayer.frame = view.frame in viewDidLoad, but at that point, the view's frame isn't finalized (especially if you're using Auto Layout). This can cause the gradient to be cut off or misaligned on different devices.
Update your ViewHelper method to make the gradient layer resize with the view:
class ViewHelper { class func BackGroundColor(view: UIView){ let startColor = UIColor(red:0.15, green:0.50, blue:0.70, alpha:1.0) let endColor = UIColor(red:0.58, green:0.65, blue:0.81, alpha:1.0) let newLayer = CAGradientLayer() newLayer.colors = [startColor.cgColor,endColor.cgColor] newLayer.zPosition = -1 newLayer.frame = view.bounds // Use bounds instead of frame for better alignment // Make the layer automatically resize with the view newLayer.autoresizingMask = [.layerWidthSizable, .layerHeightSizable] view.layer.addSublayer(newLayer) } }
3. Quick Check for Navigation Controller Setup
Just to be thorough: ensure your initial view controller is embedded in a UINavigationController. If self.navigationController is nil in your ifLogged() function, the push won't happen—but since you're seeing the background color, this is probably already set up correctly.
内容的提问来源于stack exchange,提问作者Jez

