0

i have a View controller containing 2 Views, one of which contains a button. Both Views have contraints for proportional height, aspect ratio, aligning their center X(with multiplier) and bottom space(with multiplier) to superview. they both start with alpha = 0 and i fade them in with:

override func viewDidAppear(_ animated: Bool) {
    UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
        self.infoContainer.alpha = 1.0
        self.infoContainer.frame.origin.y -= UIScreen.main.bounds.height * 0.25

        self.ordersContainer.alpha = 1.0
        self.ordersContainer.frame.origin.y -= UIScreen.main.bounds.height * 0.25
    }, completion: nil)
}

now this worked with no problem, but now i need to change the text size of the mentioned button depending on Screen Resolution. So, i created a class:

class ScaledButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        var att = titleLabel?.attributedText?.attributes(at: 0, effectiveRange: nil)
        let font = att?[NSAttributedStringKey.font] as? UIFont
        let size = font?.pointSize
        att?[NSAttributedStringKey.font] = UIFont(name: (font?.fontName)!, size: size! * ScaledLabel.scaleFactor)
        if(titleLabel?.text == nil) {return}
        setAttributedTitle(NSAttributedString(string: (titleLabel?.text)!, attributes: att), for: .normal)

    }
}

the static factor ScaledLabel.scaleFactor is calculated elsewhere. Now, when i set this class for the button in the second view, instead of starting at the position they are in the storyboard and ending the way they should, they start correctly(i believe) are moved to the correct postion instantly, then to immediatly to a postion further down and then with animation to the tsart position.... If i remove the line with setAttributedTitle from my class it works, but obviously thats not an option. What is wrong here?

dwjohnston
  • 11,163
  • 32
  • 99
  • 194
Ginso
  • 459
  • 17
  • 33
  • Did you consider to use [`NSLayoutConstraint`](https://developer.apple.com/reference/uikit/nslayoutconstraint) and animate the constant value change, rather than manipulate the `frame` of the button? [Here](https://www.raywenderlich.com/5255-basic-uiview-animation-tutorial-getting-started) is a good tutorial to get started. – carsten Feb 26 '19 at 12:20
  • when i change the constant instead of the frame, the change happens immediatly and not animated – Ginso Feb 26 '19 at 12:27
  • Maybe this will help with the constraint not animating: https://stackoverflow.com/a/12926646/576932 – ford Feb 26 '19 at 12:40

0 Answers0