SceneKit场景中按钮正常显示但标签不显示的问题求助
Fixing UILabel Not Showing in SceneKit SCNView
Hey there! Let's troubleshoot why your UILabel isn't appearing alongside your working UIButton in the SCNView. Looking at your code, there are a few key layout issues we can fix to get your label showing up correctly:
Key Issues in Your Current Code
- Frame +
sizeToFit()Order: You first set a narrow width (100pt) and fixed height (25pt) for the label, then callsizeToFit(). This forces the label to shrink text to fit that tiny initial frame or pushes content off-screen if the text needs more space. - Inaccurate Positioning: Using
self.view.frame.heightmight give incorrect values if the view hasn't finished laying out (e.g., if you're calling this inviewDidLoad). - Misused
isEnabledFlag: SettingisEnabled = falsedisables user interaction, not visibility — it’s easy to confuse this with a visibility toggle, so remove it unless you specifically need to block taps on the label.
Modified Label Code
Replace your existing expLabel setup with this adjusted version:
let expLabel = UILabel() expLabel.text = "Lütfen geliştirmek istediğiniz kas grubunu seçiniz" expLabel.textColor = .black expLabel.textAlignment = .center expLabel.lineBreakMode = .byWordWrapping expLabel.numberOfLines = 0 // 1. Set a proper width first (account for 20pt padding on left/right) expLabel.frame = CGRect(x: 20, y: 0, width: self.view.bounds.width - 40, height: 0) // 2. Let sizeToFit() calculate the correct height based on text and width expLabel.sizeToFit() // 3. Position the label at the bottom, with 20pt padding from the edge expLabel.center.x = self.view.bounds.midX expLabel.frame.origin.y = self.view.bounds.height - expLabel.frame.height - 20 // 4. Add autoresizing masks to keep the label aligned during rotation/size changes expLabel.autoresizingMask = [.flexibleWidth, .flexibleTopMargin] // Remove this line unless you need to disable user interaction (not visibility) // expLabel.isEnabled = false scnView.addSubview(expLabel)
Additional Tips
- Call
createSCNViewat the Right Time: If you're currently calling this function inviewDidLoad, move it toviewDidAppear(_:)orviewDidLayoutSubviewsinstead. This ensuresself.view.boundshas accurate dimensions for positioning the label. - Check Z-Position (If Needed): If the label still doesn't show up, force it to the front with
expLabel.layer.zPosition = 10(higher values mean it’s drawn on top of other content).
With these changes, your label should now display correctly at the bottom of your SCNView, wrap text properly, and stay in place when the view resizes or rotates.
内容的提问来源于stack exchange,提问作者Muhammet Taha Genç
相关产品推荐
相关产品推荐

