I need some help to translate the rightBarButtonItem in the UINavigationBar. I have followed this and also found similar questions which lead me to add the button as a customView of a UIBarButtonItem, however the code is still not working.
I have created a UIBarButtonItem and set a UIButton as its custom view.
let button: UIButton = {
let b = UIButton(type: .system)
b.setTitle("Test", for: .normal)
return b
}()
lazy var barButton = UIBarButtonItem(customView: button)
This method is called from viewDidLoad
func configNavBar() {
navigationItem.rightBarButtonItem = barButton
}
To animate the bar button when some action occurs:
func animateRightBarButton() {
UIView.animate(withDuration: 0.25,
delay: 0,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 1.5,
options: .curveEaseInOut,
animations: {
guard let customView = self.barButton.customView else {return}
var x: CGFloat = customView.transform.tx == 0 ? 100 : 0
customView.transform = CGAffineTransform(scaleX: x, y: 0)
}, completion: nil)
}
The bar button appears as expect when the view has loaded. I want the button to slide off screen but i have noticed a couple of problems.
The button disappears from the view with no animation when this function is called.
If I print(x) (the translate x value) it remains at 100.0 and does not change back to 0 using the ternary operator. Any help appreciated to get this working, thanks.