You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Frame + sizeToFit() Order: You first set a narrow width (100pt) and fixed height (25pt) for the label, then call sizeToFit(). This forces the label to shrink text to fit that tiny initial frame or pushes content off-screen if the text needs more space.
  2. Inaccurate Positioning: Using self.view.frame.height might give incorrect values if the view hasn't finished laying out (e.g., if you're calling this in viewDidLoad).
  3. Misused isEnabled Flag: Setting isEnabled = false disables 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 createSCNView at the Right Time: If you're currently calling this function in viewDidLoad, move it to viewDidAppear(_:) or viewDidLayoutSubviews instead. This ensures self.view.bounds has 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ç

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:42:57