<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>UITabBar on entangledDEV</title>
        <link>/tags/uitabbar/</link>
        <description>Recent content in UITabBar on entangledDEV</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <lastBuildDate>Sun, 01 Feb 2026 00:09:25 -0600</lastBuildDate><atom:link href="/tags/uitabbar/index.xml" rel="self" type="application/rss+xml" /><item>
            <title>Programmatic Tab Bar</title>
            <link>/p/programmatic-tab-bar/</link>
            <pubDate>Sun, 26 May 2024 22:17:06 +0000</pubDate>
            <guid>/p/programmatic-tab-bar/</guid>
            <description>&lt;p&gt;Tabs bars are one the most recognizable UI elements for an iOS apps. We can find this element in build in apps like Phone, Photos, Music, AppStore, and many more.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;    &lt;img src=&#34;/posts/ProgrammaticTabBar/tabbar-example.png&#34;&#xA;        loading=&#34;lazy&#34;&#xA;        &#xA;            alt=&#34;tab-bar-example&#34;&#xA;        &#xA;    &gt;&lt;/p&gt;&#xA;&lt;p&gt;Lucky for us, this element is quite easy to use, and in this tutorial, we&amp;rsquo;ll learn how to create a tab bar programmatically using UIKit. Let&amp;rsquo;s start!&lt;/p&gt;&#xA;&lt;h1 id=&#34;simple-example&#34;&gt;&lt;a href=&#34;#simple-example&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Simple example&#xA;&lt;/h1&gt;&lt;p&gt;Let&amp;rsquo;s start by configuring our project to allow us to instantiate our views programmatically. You can follow &lt;a class=&#34;link&#34; href=&#34;/p/configuring-a-programmatic-uikit-project/&#34; &gt;this guide&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;1-create-the-tab-bar-class&#34;&gt;&lt;a href=&#34;#1-create-the-tab-bar-class&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;1. Create the Tab Bar class&#xA;&lt;/h2&gt;&lt;p&gt;I prefer to have a separate class that handle all the Tab Bar related responsibilities, so let&amp;rsquo;s create a new file called &lt;code&gt;TabBarViewController.swift&lt;/code&gt; and add the following code.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TabBarController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITabBarController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is the minimum code we need to have a working tab bar.&lt;/p&gt;&#xA;&lt;h2 id=&#34;2-adding-the-tab-bar-to-the-app&#34;&gt;&lt;a href=&#34;#2-adding-the-tab-bar-to-the-app&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;2. Adding the tab bar to the app&#xA;&lt;/h2&gt;&lt;p&gt;Now we can instantiate this as we would do with a &lt;code&gt;UINavigationController&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Open &lt;code&gt;SceneDelegate.swift&lt;/code&gt; and and update the &lt;code&gt;willConnectTo&lt;/code&gt; method as follows:&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;scene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;scene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;willConnectTo&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;session&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UISceneSession&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;connectionOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ConnectionOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;windowScene&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;scene&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIWindowScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIWindow&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;windowScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;windowScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TabBarController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;makeKeyAndVisible&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We are assigning the new Tab Bar as the window root view controller. This will present this as the first element in our screen.&lt;/p&gt;&#xA;&lt;p&gt;Now run the app, you should see an empty tab bar in the screen.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;    &lt;img src=&#34;/posts/ProgrammaticTabBar/tab-bar-bare-minimum.png&#34;&#xA;        loading=&#34;lazy&#34;&#xA;        &#xA;            alt=&#34;bare-tab-bar&#34;&#xA;        &#xA;    &gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;3-creating-the-view-controllers-to-show&#34;&gt;&lt;a href=&#34;#3-creating-the-view-controllers-to-show&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;3. Creating the View Controllers to show&#xA;&lt;/h2&gt;&lt;p&gt;We need to have some view controllers to show on the screen. Let&amp;rsquo;s make two simple dummy ones.&lt;/p&gt;&#xA;&lt;p&gt;Create two new files, &lt;code&gt;AViewController.swift&lt;/code&gt; and &lt;code&gt;BViewController.swift&lt;/code&gt; and add the following code:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// A View Controller&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;AViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;backgroundColor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;yellow&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// B View Controller&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;backgroundColor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;orange&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;These two view controllers are quite simple, with the only difference being their background color.&lt;/p&gt;&#xA;&lt;h2 id=&#34;4-presenting-the-view-controllers-in-the-tab-bar&#34;&gt;&lt;a href=&#34;#4-presenting-the-view-controllers-in-the-tab-bar&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;4. Presenting the View Controllers in the Tab Bar&#xA;&lt;/h2&gt;&lt;p&gt;Each &lt;code&gt;UIViewController&lt;/code&gt; has a property called &lt;code&gt;tabBarItem&lt;/code&gt; to which we can assign the tab bar item it will display in a tab bar.&lt;/p&gt;&#xA;&lt;p&gt;Open the &lt;code&gt;TabBarController.swift&lt;/code&gt; and update the code as follows:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TabBarTag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TabBarController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITabBarController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;setupTabBar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// 3. Call the new function&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// 1 .Add this private extension&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TabBarController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// 2. Add this function that will instantiate the ViewControllers and assign their tab bar items&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;setupTabBar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;aViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;AViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Adding the tab bar item to show in the tab bar for AViewController&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;aViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tabBarItem&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITabBarItem&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;A&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;image&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;systemName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;1.lane&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;tag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TabBarTag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;hashValue&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;bViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Adding the tab bar item to show in the tab bar for BViewController&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;bViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tabBarItem&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITabBarItem&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;B&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;image&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;systemName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;2.lane&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;tag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TabBarTag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;hashValue&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// UITabBarController contains an array of the view controllers that will be showing in the screen&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewControllers&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;aViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;bViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I’m using an enum for the tag, so that we can refer to each tab bar item easily in the future for other parts of our app.&lt;/p&gt;&#xA;&lt;h2 id=&#34;5-run-the-app&#34;&gt;&lt;a href=&#34;#5-run-the-app&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;5. Run the app&#xA;&lt;/h2&gt;&lt;p&gt;Now run the app and see the tab bar in action.&lt;/p&gt;&#xA;&lt;p&gt;With this little code, we have a simple tab bar implemented in our app.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;    &lt;img src=&#34;/posts/ProgrammaticTabBar/simple-tab-bar.gif&#34;&#xA;        loading=&#34;lazy&#34;&#xA;        &#xA;            alt=&#34;simple-tab-bar&#34;&#xA;        &#xA;    &gt;&lt;/p&gt;&#xA;&lt;h1 id=&#34;advanced-usage&#34;&gt;&lt;a href=&#34;#advanced-usage&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Advanced Usage&#xA;&lt;/h1&gt;&lt;p&gt;Under development &amp;hellip;&lt;/p&gt;&#xA;&lt;!-- We can customize a lot about the tab bar, from appearance to behavior, let&#39;s explore some of this advance uses.&#xA;&#xA;## Customizing the Tab Bar Appearance&#xA;&#xA;## 1. Simple background opacity and color&#xA;&#xA;To make our tab bar solid again and change its background color you can use this code:&#xA;&#xA;```swift&#xA;import UIKit&#xA;&#xA;// Enum omitted for brevity&#xA;&#xA;final class TabBarController: UITabBarController {&#xA;    override func viewDidLoad() {&#xA;        super.viewDidLoad()&#xA;        setupTabBar()&#xA;        tabBarCustomization() // run the customization function&#xA;    }&#xA;}&#xA;&#xA;private extension TabBarController {&#xA;    &#xA;    func setupTabBar() {&#xA;        // Same code for setting up&#xA;    }&#xA;    &#xA;    // Add this function to change some customizations&#xA;    func tabBarCustomization() {&#xA;        let appearance = UITabBarAppearance()&#xA;        appearance.configureWithOpaqueBackground()&#xA;        appearance.backgroundColor = .white&#xA;        &#xA;        tabBar.standardAppearance = appearance&#xA;        if #available(iOS 15.0, *) {&#xA;            tabBar.scrollEdgeAppearance = appearance&#xA;        }&#xA;        &#xA;        tabBar.tintColor = .systemBlue&#xA;        tabBar.unselectedItemTintColor = .gray&#xA;    }&#xA;}&#xA;```&#xA;&#xA;The `tabBarCustomization` function is responsible for customizing the appearance of the tab bar. Here&#39;s a breakdown of what each line does:&#xA;&#xA;- `let appearance = UITabBarAppearance()`&#xA;  - Creates a new instance of `UITabBarAppearance` to define the appearance of the tab bar.&#xA;&#xA;- `appearance.configureWithOpaqueBackground()`&#xA;  - Configures the appearance object to use an opaque background. This is a convenient method to set up default properties for an opaque appearance.&#xA;&#xA;- `appearance.backgroundColor = .white`&#xA;  - Sets the background color of the tab bar to white.&#xA;&#xA;- `tabBar.standardAppearance = appearance`&#xA;  - Applies the configured appearance to the standard tab bar appearance. This affects how the tab bar looks in its default state.&#xA;&#xA;- `if #available(iOS 15.0, *) { tabBar.scrollEdgeAppearance = appearance }`&#xA;  - For iOS 15 and later, sets the `scrollEdgeAppearance` to the same appearance. This ensures that the tab bar maintains a consistent appearance even when it is at the edge of a scrollable content area.&#xA;&#xA;- `tabBar.tintColor = .systemBlue`&#xA;  - Sets the tint color of the tab bar items to system blue. This affects the color of the selected tab bar item.&#xA;&#xA;- `tabBar.unselectedItemTintColor = .gray`&#xA;  - Sets the tint color of the unselected tab bar items to gray. This affects the color of the tab bar items that are not selected.&#xA;&#xA;Here is the resulting tab bar:&#xA;&#xA;![simple-opacity-color](/posts/ProgrammaticTabBar/simple-opacity-color.png)&#xA;&#xA;## 2. Using New Textures and Materials&#xA;&#xA;To apply the new textures like `ultraThinMaterial`, you can add a `UIVisualEffectView` with the desired blur effect to the tab bar’s background.&#xA;&#xA;```swift&#xA;import UIKit&#xA;&#xA;// Enum omitted for brevity&#xA;&#xA;final class TabBarController: UITabBarController {&#xA;    override func viewDidLoad() {&#xA;        super.viewDidLoad()&#xA;        setupTabBar()&#xA;        tabBarMaterials() // run the customization function&#xA;    }&#xA;}&#xA;&#xA;private extension TabBarController {&#xA;    &#xA;    func setupTabBar() {&#xA;        // Same code for setting up&#xA;    }&#xA;    &#xA;    // Add this function to change some customizations&#xA;    func tabBarMaterials() {&#xA;        let appearance = UITabBarAppearance() // a&#xA;        appearance.configureWithOpaqueBackground() // b&#xA;        appearance.backgroundColor = .clear // c&#xA;&#xA;        let itemAppearance = UITabBarItemAppearance() // d&#xA;&#xA;        // Customize normal state&#xA;        itemAppearance.normal.iconColor = .gray // e&#xA;        itemAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.gray] // f&#xA;        itemAppearance.normal.badgeBackgroundColor = .red // g&#xA;&#xA;        // Customize selected state&#xA;        itemAppearance.selected.iconColor = .systemBlue // h&#xA;        itemAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.systemBlue] // i&#xA;        itemAppearance.selected.badgeBackgroundColor = .systemRed // j&#xA;&#xA;        appearance.stackedLayoutAppearance = itemAppearance // k&#xA;&#xA;        tabBar.standardAppearance = appearance // l&#xA;        if #available(iOS 15.0, *) {&#xA;            tabBar.scrollEdgeAppearance = appearance // m&#xA;        }&#xA;&#xA;        tabBar.tintColor = .systemBlue // n&#xA;        tabBar.unselectedItemTintColor = .gray // o&#xA;&#xA;        // Add visual effect view for ultraThinMaterial&#xA;        let blurEffect = UIBlurEffect(style: .systemUltraThinMaterial) // p&#xA;        let blurEffectView = UIVisualEffectView(effect: blurEffect) // q&#xA;        blurEffectView.frame = tabBar.bounds // r&#xA;        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // s&#xA;        tabBar.insertSubview(blurEffectView, at: 0) // t&#xA;    }&#xA;}&#xA;```&#xA;Here is what each part of the code is doing (refer to the letter in comment):&#xA;&#xA;- **a**: `let appearance = UITabBarAppearance()`&#xA;  - Creates a new instance of `UITabBarAppearance`, which defines the overall appearance of the tab bar.&#xA;- **b**: `appearance.configureWithOpaqueBackground()`&#xA;  - Configures the appearance object to use an opaque background. This sets up default properties for an opaque appearance.&#xA;- **c**: `appearance.backgroundColor = .clear`&#xA;  - Sets the background color of the tab bar to clear. This allows the visual effect view to be visible.&#xA;- **d**: `let itemAppearance = UITabBarItemAppearance()`&#xA;  - Creates a new instance of `UITabBarItemAppearance` to define the appearance of tab bar items.&#xA;&#xA;- **e**: `itemAppearance.normal.iconColor = .gray`&#xA;  - Sets the icon color of the tab bar items when they are not selected to gray.&#xA;- **f**: `itemAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.gray]`&#xA;  - Sets the text color of the tab bar items when they are not selected to gray.&#xA;- **g**: `itemAppearance.normal.badgeBackgroundColor = .red`&#xA;  - Sets the badge background color of the tab bar items when they are not selected to red.&#xA;&#xA;- **h**: `itemAppearance.selected.iconColor = .systemBlue`&#xA;  - Sets the icon color of the tab bar items when they are selected to system blue.&#xA;- **i**: `itemAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.systemBlue]`&#xA;  - Sets the text color of the tab bar items when they are selected to system blue.&#xA;- **j**: `itemAppearance.selected.badgeBackgroundColor = .systemRed`&#xA;  - Sets the badge background color of the tab bar items when they are selected to system red.&#xA;&#xA;- **k**: `appearance.stackedLayoutAppearance = itemAppearance`&#xA;  - Applies the customized item appearance to the `stackedLayoutAppearance` of the `UITabBarAppearance`. This ensures that the tab bar items have the desired appearance in both normal and selected states.&#xA;&#xA;- **l**: `tabBar.standardAppearance = appearance`&#xA;  - Applies the configured appearance to the standard tab bar appearance, affecting how the tab bar looks in its default state.&#xA;- **m**: `if #available(iOS 15.0, *) { tabBar.scrollEdgeAppearance = appearance }`&#xA;  - For iOS 15 and later, sets the `scrollEdgeAppearance` to the same appearance. This ensures that the tab bar maintains a consistent appearance even when it is at the edge of a scrollable content area.&#xA;&#xA;- **n**: `tabBar.tintColor = .systemBlue`&#xA;  - Sets the tint color of the selected tab bar item to system blue.&#xA;- **o**: `tabBar.unselectedItemTintColor = .gray`&#xA;  - Sets the tint color of the unselected tab bar items to gray.&#xA;&#xA;- **p**: `let blurEffect = UIBlurEffect(style: .systemUltraThinMaterial)`&#xA;  - Creates a new blur effect with the `systemUltraThinMaterial` style, which provides a modern, translucent look.&#xA;- **q**: `let blurEffectView = UIVisualEffectView(effect: blurEffect)`&#xA;  - Creates a visual effect view using the blur effect.&#xA;- **r**: `blurEffectView.frame = tabBar.bounds`&#xA;  - Sets the frame of the visual effect view to cover the entire tab bar.&#xA;- **s**: `blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]`&#xA;  - Ensures the visual effect view resizes with the tab bar.&#xA;- **t**: `tabBar.insertSubview(blurEffectView, at: 0)`&#xA;  - Inserts the visual effect view into the tab bar&#39;s subview hierarchy, at the bottom layer, so it appears behind the tab bar items.&#xA;&#xA;&#xA;# Conclusion&#xA;&#xA;Creating a tab bar programmatically in UIKit is straightforward and offers great flexibility for customizations. By following this guide, you can build a robust tab-based navigation for your iOS apps. Stay tuned for more tutorials in the “UIKit Learning” series. --&gt;</description>
        </item></channel>
</rss>
