Upload View under TabBar Controller in swift – iOS

On this put up we’re going to learn to upload a subview below UITabBarController in swift 5.

Add View below TabBar Controller in swift - iOS

First obtain the starter mission from the under hyperlink:

Open the mission and run, we will be able to  see as  under pictures within the simulator.

Add View below TabBar Controller in swift - iOS
On this mission we’ve got House View controller to show listing of puts, Element view controller for position element & Account view controller for 2d tab.
Let’s dive into the objective.
First create new swift report subclass of UIView, title it TabBarSwitchView.swift.
On this view we’re going upload transfer. 

Substitute TabBarSwitchView magnificence with the next code:

import UIKit

magnificence TabBarSwitchView: UIView

let onColor = UIColor(hex: 0xF9CF2D)

let offColor = UIColor.white

let deselectColor = UIColor(hex: 0xBFBFBF)

let bgColor = UIColor(hex: 0x404040)

var travellerLabel: UILabel = ()

var hostLabel: UILabel = ()

var switchButton: UISwitch =
let v = UISwitch()
v.isUserInteractionEnabled = false
v.translatesAutoresizingMaskIntoConstraints = false
go back v

var ison: Bool = false
isTraveller = !ison
switchButton.setOn(ison, animated: true)
if ison else

override init(body: CGRect)

let scale:CGFloat = 0.6

func setupViews()

override func layoutSubviews()

required init?(coder aDecoder: NSCoder)

Subsequent create new view controller and title it as TabBarRootVC.swift.

Substitute code with following code:

import UIKit

var isTraveller = false
magnificence TabBarRootVC: UIViewController {

personal lazy var tabBarVC: CustomTabBarVC =
var viewController = CustomTabBarVC()
go back viewController

var bottomHeight: CGFloat
guard #to be had(iOS 11.0, *),
let window = UIApplication.shared.keyWindow else
go back window.safeAreaInsets.backside

var switchView: TabBarSwitchView =
let v = TabBarSwitchView()
v.translatesAutoresizingMaskIntoConstraints = false
go back v

var hideBottomBar: Bool = false

var heightConstraint: NSLayoutConstraint!

override func viewDidLoad()

func setupViews()
self.view.backgroundColor = UIColor.white
switchView.edges([.left, .right, .bottom], to: self.view, offset: .0)
heightConstraint = switchView.heightAnchor.constraint(equalToConstant: 32 + bottomHeight)
heightConstraint.isActive = true
switchView.ison = false
switchView.isUserInteractionEnabled = true
switchView.addGestureRecognizer(UITapGestureRecognizer(goal: self, motion: #selector(switchViewTapped(recognizer:))))

tabBarVC.view.translatesAutoresizingMaskIntoConstraints = false
tabBarVC.view.edges([.left, .right, .top], to: self.view, offset: .0)
tabBarVC.view.bottomAnchor.constraint(equalTo: switchView.topAnchor, consistent: 0).isActive = true
tabBarVC.didMove(toParent: self)
if !hideBottomBar
heightConstraint.consistent = 32 + bottomHeight

@objc func switchViewTapped(recognizer: UITapGestureRecognizer)
switchView.ison = !switchView.ison
isTraveller = !switchView.ison
NotificationCenter.default.put up(title: Notification.Identify(rawValue: "switchChanged"), object: nil)


Open utilities report and upload the next extension :

extension UIViewController 
func hideBottomSwitch(_ disguise: Bool)

Open CustomTabBarVC.swift report and upload the next code sooner than viewDidLoad() :

var hideSwitch: Bool = false 

Open the HomeViewController.swift report and change all code with the next:
import UIKit

magnificence HomeViewController: UIViewController

extension HomeViewController: UITableViewDelegate

extension HomeViewController: UITableViewDataSource
Now run the mission we will be able to see output as following:

Add View below TabBar Controller in swift - iOS
Obtain the completed mission from the next hyperlink: