如何使用 Swift 3 在 BWWalkthrough View Controller 中添加标签?
How to add Label in BWWalkthrough View Controller with Swift 3?
我在我的应用程序中使用 BWWalkthrough 库 作为幻灯片图像。我 在每张幻灯片中添加标题和消息标签。
我想翻译到每个标签。
因此,我将标签拖到 IBOutlet
并在 ViewDidLoad
中添加 NStranslation 文本。
但是,当我 运行 代码时,出现致命错误。
这是我的代码。
在 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:", 评论: "")
有人能帮帮我吗?
执行如下操作,将在所有页面中添加一个标签,您可以像这样添加更多标签。
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)
}
}
}
我在我的应用程序中使用 BWWalkthrough 库 作为幻灯片图像。我 在每张幻灯片中添加标题和消息标签。
我想翻译到每个标签。
因此,我将标签拖到 IBOutlet
并在 ViewDidLoad
中添加 NStranslation 文本。
但是,当我 运行 代码时,出现致命错误。
这是我的代码。
在 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:", 评论: "")
有人能帮帮我吗?
执行如下操作,将在所有页面中添加一个标签,您可以像这样添加更多标签。
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)
}
}
}