Animate a UIBarButtonItem using Swift
Before we start
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12.2, and Swift 5.3
Keywords and Terminology
UINavigationBar: A bar containing buttons for navigating within the hierarchy of screens
I’ve always wanted to create great user experiences. One way of doing this is to put lovely animations onto your UI. The
UINavigationBar is a great opportunity for that.
I’ve prepared a Repo
I started off the project and in the storyboard embedded the
UIViewController instance in a
UINavigationController, and then a
UIBarButtonItem is added using the storyboard.
The image can then be chosen using the storyboard. Select the storyboard, and then find bar button item in the components by choosing with + and selecting the bar button item
this can then be dragged into the
Set images to be rendered as original image in the asset catalogue to stop them being blue. This can be done by selecting the asset catalog, then the asset and Image Set has the option on the right-hand side of Render As> Original Image.
Size of the images
For the bar button items the best sizes are
22–44–66 for items
The Solution: Explanation
It is possible to place any sort of
UIView subclass inside a bar button item as a custom view. It is therefore possible to place a
UIButton into the custom view property, and then perform any animations as required.