【问题标题】:Experiencing limit in number of UILabels added as subviews添加为子视图的 UILabel 数量遇到限制
【发布时间】:2019-08-01 13:27:46
【问题描述】:

无法在 iPad Playgrounds 应用中向 UIViewController 的视图添加三个以上的 UILabel。是我还是系统?

显示问题的简化代码。硬件是运行 iOS 12.3.1 Playgrounds 应用程序 3.0 的 2018 iPad Pro。使用 UIView 最多可以成功添加五个。

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    let square50 = CGSize(width: 50, height: 50)
    override func viewDidLoad() {
        var myFrame = CGRect(origin: .zero, size: square50)
        for index in 0...4 {
            view.addSubview(UIView(frame: myFrame)) // fails at 6th!
            (view.subviews[index] as! UIView).backgroundColor = .red
            myFrame.origin.y += 80
        }
    }
}
PlaygroundPage.current.liveView = MyViewController()

索引范围设置如图所示,代码按预期工作,显示设置数量的彩色矩形。随着范围增加到 0...5,运行时停止并显示消息“运行此游乐场时遇到问题。检查你的代码是否有错误”。

【问题讨论】:

  • texts 里面有 4 个项目吗?
  • 是的,如果数组中没有足够的条目,则会出现“索引超出范围”运行时错误。
  • 如果您创建一个小型 Playground 程序来通过仅添加空 UIView 矩形来测试子视图限制,则代码将在第六次尝试添加子视图时失败。
  • 你能发布那个代码吗? Stackoverflow 建议发布最小可重现示例。 stackoverflow.com/help/minimal-reproducible-example
  • 如何“发布”这 20 行代码?对不起!我已经在 Apple 开发者论坛上完成了这项工作,但我所能做的就是添加一个有限的评论!

标签: swift uikit swift-playground


【解决方案1】:

刚刚能够在 2015 年末在我的 iMac Retina 5k 27" 上在 OS X 10.16.6 下测试 Xcode 10.3 上的代码。代码没有问题,并且对子数量没有短程限制可以创建的视图。

问题在于 iOS Swift Playgrounds 3.0 在使用 iOS 12.3.1 的 12.9 英寸第三代 iPad Pro 上运行。因此这是一个错误!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    相关资源
    最近更新 更多