0

我在我的应用程序中使用BWWalkthrough 库来获取幻灯片图像。我在每张幻灯片中添加标题和消息标签。

在此处输入图像描述

我想翻译到每个标签。
所以,我将标签拖到IBOutlet并添加 NStranslation 文本ViewDidLoad
但是,当我运行代码时,我遇到了致命错误。
这是我的代码。
BWWalkthroughPageViewController.swift,

  @IBOutlet weak var lblTitle1: UILabel!


override open func viewDidLoad() {
    super.viewDidLoad()
    lblTitle1.text = NSLocalizedString("Date:", comment: "")

    self.view.layer.masksToBounds = true

    subviewsSpeed = Array()

    for v in view.subviews{
        speed.x += speedVariance.x
        speed.y += speedVariance.y
        if !notAnimatableViews.contains(v.tag) {
            subviewsSpeed.append(speed)
        }
    }
}


我在以下代码(BWWalkthroughViewController.swift)中遇到错误。

viewController.view.translatesAutoresizingMaskIntoConstraints = false

lblTitle1.text = NSLocalizedString("Date:", comment: "")

有人可以帮我吗?

4

1 回答 1

1

执行以下操作,这将在所有页面中添加一个标签,您可以以相同的方式添加更多标签。

override open func viewDidLoad() {
    super.viewDidLoad()
    self.view.layer.masksToBounds = true

    let sampleLabel:UILabel = UILabel()
    sampleLabel.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
    sampleLabel.textAlignment = .center
    sampleLabel.text = "Hello this is iOS dev"
    sampleLabel.numberOfLines = 1
    sampleLabel.textColor = .red
    sampleLabel.font=UIFont.systemFont(ofSize: 22)
    sampleLabel.backgroundColor = .yellow

    view.addSubview(sampleLabel)
    sampleLabel.translatesAutoresizingMaskIntoConstraints = false
    sampleLabel.heightAnchor.constraint(equalToConstant: 200).isActive = true
    sampleLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
    sampleLabel.centerXAnchor.constraint(equalTo: sampleLabel.superview!.centerXAnchor).isActive = true
    sampleLabel.centerYAnchor.constraint(equalTo: sampleLabel.superview!.centerYAnchor).isActive = true

    subviewsSpeed = Array()

    for v in view.subviews{
        speed.x += speedVariance.x
        speed.y += speedVariance.y
        if !notAnimatableViews.contains(v.tag) {
            subviewsSpeed.append(speed)
        }
    }
}

更新

lblTitle1您可以通过下面的安全展开检查来防止崩溃的发生。

override open func viewDidLoad() {
    super.viewDidLoad()

    if (lblTitle1) != nil {
        lblTitle1.text = NSLocalizedString("Date:", comment: "")
    }

    self.view.layer.masksToBounds = true

    subviewsSpeed = Array()

    for v in view.subviews{
        speed.x += speedVariance.x
        speed.y += speedVariance.y
        if !notAnimatableViews.contains(v.tag) {
            subviewsSpeed.append(speed)
        }
    }
}
于 2017-07-10T06:00:57.543 回答