Wednesday, June 10, 2026
HomeiOS DevelopmentThe best way to Create an Animated Navigation Menu in SwiftUI

The best way to Create an Animated Navigation Menu in SwiftUI

[ad_1]

One purpose why I actually take pleasure in programming utilizing the SwiftUI framework is that it makes really easy to animate view adjustments. Particularly, the introduction of the matchedGeometryEffect modifier, launched in iOS 14, additional simplifies the implementation of view animations. With matchedGeometryEffect, all you want is describe the looks of two views. The modifier will then compute the distinction between these two views and routinely animates the scale/place adjustments.

We’ve written a detailed tutorial on matchedGeometryEffect. I extremely advocate you to test it out if that is the very first time you come throughout this modifier. On this tutorial, we are going to make use of matchedGeometryEffect to develop an animated navigation menu just like the one proven beneath.

swiftui-animated-navigation-menu-matchedGeometryEffect

Editor’s Be aware: To dive deeper into SwiftUI animation and study extra concerning the SwiftUI framework, you may try the e-book here.

Creating the Navigation Menu

Earlier than we create the animated menu, let’s begin by creating the static model. For example, the navigation menu solely shows three menu gadgets.

navigation-menu-swiftui

To structure three textual content views horizontally with equal spacing, we use the HStack view and Spacer to rearrange the views. Right here is the code pattern:

As you may see from the code above, it incorporates various duplications. It may be additional simplified with ForEach:

We added a state variable named selectedIndex to maintain monitor of the chosen menu merchandise. When the menu merchandise is chosen, we spotlight it in purple. In any other case, its background shade is ready to mild grey.

To detect customers’ contact, we connected the .onTapGesture modifier to the textual content view. When it’s tapped, we replace the worth of selectedIndex to spotlight the chosen textual content view.

Animating the Navigation Menu

Now that we’ve applied the navigation menu, nevertheless, it misses the required animation. To animating the view change each time a menu merchandise is chosen, all we have to do is create a namespace variable and connect the matchedGeometryEffect modifier to the textual content view in purple:

The ID and namespace are used for figuring out which views are a part of the identical transition. We additionally want to connect the .animation modifier to the HStack view to allow the view animation. Be aware that this challenge is constructed utilizing Xcode 13. The animation modifier is up to date within the new model of iOS. You need to present the worth to observe for adjustments. Right here, it’s the selectedIndex.

When you made the adjustments, you may check the NavigationMenu view in a simulator. Faucet a menu merchandise and you will note a pleasant animation when the merchandise is transited from one state to a different.

animated-swiftui-navigation-menu

Utilizing the Animated Navigation Menu View

To use this animated navigation menu to your challenge, you may modify the NavigationMenu view to just accept a binding to the chosen index:

For instance, you’ve got created a page-based tab view like this:

You may add the NavigationMenu view as an overlay and use your personal menu gadgets.

swiftui-tab-view-animated-tab-bar


Founding father of AppCoda. Creator of a number of iOS programming books together with Starting iOS Programming with Swift and Mastering SwiftUI. iOS App Developer and Blogger. Observe me at Fb, Twitter and Google+.



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments