Silver Challenge Solution with UILayoutGuides only


#1

To solve the challenge with UILayoutGuides only the following two methods must be changed to be like that:

func updateOffScreenLabel() {
    let layoutGuide = UILayoutGuide()
    view.addLayoutGuide(layoutGuide)
    layoutGuide.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    layoutGuide.trailingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    nextQuestionLabel.centerXAnchor.constraint(equalTo: layoutGuide.centerXAnchor).isActive = true
}

func animateLabelTransitions() {
    view.layoutIfNeeded() //Forces any outstanding layout changes to occur

    nextQuestionLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    let layoutGuide = UILayoutGuide()
    view.addLayoutGuide(layoutGuide)
    layoutGuide.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    layoutGuide.leadingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    currentQuestionLabel.centerXAnchor.constraint(equalTo: layoutGuide.centerXAnchor).isActive = true
    
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.1, options: [.curveLinear], animations: {
        self.currentQuestionLabel.alpha = 0
        self.nextQuestionLabel.alpha = 1
        self.view.layoutIfNeeded()
    }) { _ in
        swap(&self.currentQuestionLabel, &self.nextQuestionLabel)
        swap(&self.currentQuestionLabelCenterXConstraint, &self.nextQuestionLabelCenterXConstraint)
        self.updateOffScreenLabel()
    }
}

Anyway the old way works fine too since iOS 10. Maybe the problem mentioned by the authors was in the previous SDKs. Even changing nextQuestionLabel.alpha = 1 in viewWillAppear() didn’t show the nextQuestionLabel on the screen. The value of the view.bounds.width property must change as soon as the screen changes including the rotation.


#2

im so lost i am trying to get to know the forum on how it works


#3

You post here your questions and check for solutions.