🥸 Using UIKit's New UITab Class with Sidebar on iOS 18 👌
Need to capture HTTPS traffic?
Try Proxyman ❤️ the best-in-class macOS app, and capture HTTPS traffic from the iOS, Simulator with 1 click. Used by 250,000+ developers. Try it for free now!
Welcome to issue #35 of the iOS Coffee Break Newsletter 📬.
Last week, I implemented a sample application of the newsletter to showcase how you can use iOS 18's new TabView with a sidebar in SwiftUI.
This week, I plan to rewrite last week's application using UIKit, demonstrating how to implement the same functionality for those still working with UIKit in their projects.
Let's see how to get it done!
UIKit Integration
In UIKit, managing and customizing tabs is simple:
- UITabBarController: Use the tabs property with UITab options like UISearchTab.
- Adjusting modes: Set UITabBarController.Mode.tabBar for a standard tab bar or configure it for a sidebar layout.
- SwiftUI equivalents: Convert TabSections into UITabGroup, and use
sidebarActions(content:)
instead ofsectionActions
. - Drag and drop support: Implement UITabBarControllerDelegate to handle drag-and-drop interactions effectively.
Setting Up the Tabs
We need to create a UITabBarController and assign an array of UITab options like UISearchTab objects to its tabs property. To add a search tab, we just need to create a UISearchTab instance and add it to the tabs array.
class AppViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Assign an array of tabs.
self.tabs = [
UITab(
title: "Issues",
image: UIImage(systemName: "book"),
identifier: "issues"
) { _ in
// Return the view controller that the tab displays.
UIHostingController(rootView: IssuesView())
},
UITab(
title: "About",
image: UIImage(systemName: "person"),
identifier: "about"
) { _ in
// Return the view controller that the tab displays.
UIHostingController(rootView: AboutView())
},
// Create a search tab.
UISearchTab { _ in
UIHostingController(rootView: SearchArticlesView())
}
]
}
}
Integrate the Tab bar and Sidebar
To enable users to switch between a traditional tab bar and a sidebar, we can control how the system presents your tabs by configuring the mode property of the UITabBarController object.
override func viewDidLoad() {
super.viewDidLoad()
[...]
// Enable the sidebar.
self.mode = .tabSidebar
}
By default, the system presents the sidebar in landscape orientation and hides it in portrait orientation, however, you can toggle between the tab bar and sidebar in either orientation. You can also programmatically show and hide the sidebar by setting its
isHidden
property.
Here is the final result:
To explore the full implementation of the sample application, visit the repository on GitHub.
The improvements to tabs and sidebars in iPadOS and other Apple platforms provide greater flexibility and usability for app development. Whether you are working with SwiftUI or UIKit, I am sure these updates simplify the process of building seamless, intuitive user interfaces!
CURATED FROM THE COMMUNITY
🧱 Simple Modularization Setup for a New App
Modularizing an app from the get-go has many benefits! This week, Manu shared a straightforward yet effective approach to modularizing your iOS app using Swift Package Manager's local packages that provides a good balance between structure and simplicity.
In fact, at work, we recently migrated a monolithic codebase to a modularized solution with Swift Packages, which significantly improved our build and compilation times, so I could not recommend it more!
🎸 Music recognition with ShazamKit
ShazamKit opens up exciting possibilities for integrating music recognition into your app!
I really enjoy Artem's writing style, and this time, he put together a great guide on setting up ShazamKit, detecting songs, and managing a library of identified tracks. If you are working with music recognition, this resource could be incredibly valuable!
🥋 An Ode to Swift Enums: The View Models That Could
Swift Enums are an essential tool in every iOS developer's toolkit! Actually, they are one of my favorite features in Swift, offering a powerful way to write more expressive and efficient code.
Using a basketball practice planner app as an example, Jordan dives into the versatility of Swift enums, demonstrating how he tackled different scenarios using only this type.