如何使用 Swift 3 在 BWWalkthrough 视图控制器中添加标签? [英] How to add Label in BWWalkthrough View Controller with Swift 3?

查看:23
本文介绍了如何使用 Swift 3 在 BWWalkthrough 视图控制器中添加标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 BWWalkthrough 库 用于我的应用中的幻灯片图像.我在每张幻灯片中添加标题和消息标签.

I use BWWalkthrough library for slides images in my app. I add Title and Message labels in each slide.



我想翻译到每个标签.
因此,我将标签拖到 IBOutlet 并在 ViewDidLoad 中添加 NStranslation 文本.
但是,当我运行代码时,出现了致命错误.
这是我的代码.
BWWalkthroughPageViewController.swift 中,



I would like to translate to each labels.
So, I drag the label to IBOutlet and I add NStranslation text in ViewDidLoad.
But, when I run the code, I got fatal error.
Here is my code.
In 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

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

有人可以帮我吗?

推荐答案

做如下操作,会在所有页面添加一个标签,你可以用同样的方式添加更多标签.

Do something like below, that will add one label in all page, you can add more label like same way.

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 检查来防止崩溃.

You can prevent crash to happening by safe unwrapping lblTitle1 check below.

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)
        }
    }
}

这篇关于如何使用 Swift 3 在 BWWalkthrough 视图控制器中添加标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆