如何使用Swift 3在BWWalkthrough View Controller中添加Label? [英] How to add Label in BWWalkthrough View Controller with Swift 3?
问题描述
我在我的应用中为幻灯片图片使用 BWWalkthrough library 。我在每张幻灯片中 添加标题和消息标签 。
I use BWWalkthrough library for slides images in my app. I add Title and Message labels in each slide.
我想翻译强>每个标签。
所以,我将标签拖到 IBOutlet
,然后在 ViewDidLoad $ c $中添加NStranslation文本C>。
但是,当我运行代码时,我遇到了致命的错误。
这是我的代码。
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
和 lblTitle1.text = NSLocalizedString(日期:,评论:)
有人可以帮我吗?
推荐答案
执行以下操作,在所有页面中添加一个标签,你可以用同样的方式添加更多标签。
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 View Controller中添加Label?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!