0

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

enter image description here

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


I got error in these following codes (BWWalkthroughViewController.swift).

viewController.view.translatesAutoresizingMaskIntoConstraints = false

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

Could anyone help me please?

May Phyu
  • 895
  • 3
  • 23
  • 47

1 Answers1

1

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

Update

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)
        }
    }
}
Bhavesh Dhaduk
  • 1,888
  • 15
  • 30
  • check my updated answer and let me know if any issue @MayPhyu – Bhavesh Dhaduk Jul 10 '17 at 07:09
  • bro, your first codes work and show in the center of the screen. As my screenshot image, I would like to appear at the bottom. Now, I remove all your previous codes in ViewDidLoad and I tried with your second code. But, I got error bro – May Phyu Jul 10 '17 at 07:24
  • it work's for me, wait let me update my code @MayPhyu – Bhavesh Dhaduk Jul 10 '17 at 07:30
  • bro, I download your project and click on Run. Build Success msg show but could not run. What happen bro? – May Phyu Jul 10 '17 at 07:38
  • it's running on my side, check from your side, clean and build, delete derived data etc... @MayPhyu – Bhavesh Dhaduk Jul 10 '17 at 07:41
  • bro, so for other slides, should I check like this ? if (lblTitle1) != nil { lblTitle1.text = NSLocalizedString("Date", comment: "") } and if (lblMsg) != nil { lblMsg1.text = NSLocalizedString("Message", comment: "") } Just like this bro ? – May Phyu Jul 10 '17 at 07:54
  • @MayPhyu Yes you have to check and safe unwrapping – Bhavesh Dhaduk Jul 10 '17 at 07:56
  • bro @iOSDev, bro could you please take a look at my question https://stackoverflow.com/questions/45117730/how-to-change-label-color-inside-table-cell-with-swift-3 ? – May Phyu Jul 17 '17 at 11:19