user2569905 user2569905 - 2 months ago 7
iOS Question

UI elements not displaying in view controllers that are apart of custom tab bar controller

I am working on an iOS application that has 4 more tabs that the user needs to be able to access. I decided to create my own custom tab bar controller so I would have more options when it comes to customization. I have implemented the custom tab bar controller and am able to switch between views just fine except when I got to add UI elements such as

UITextField
,
UIButton
, etc. in my view controller classes and run the app the UI elements don't show up.

CustomTabBarController.swift

import UIKit

class CustomTabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

let socialController = socialViewController()
socialController.navigationItem.title = "Social"
let navigationController = UINavigationController(rootViewController: socialController)
navigationController.title = "Social"
navigationController.tabBarItem.image = UIImage(named: "User-Group-50")

let mapController = GoogleMapsViewController()
mapController.navigationItem.title = "Map"
let MapNavigationController = UINavigationController(rootViewController: mapController)
MapNavigationController.title = "Map"
MapNavigationController.tabBarItem.image = UIImage(named: "Map-50")

let scheduleController = scheduleViewController()
scheduleController.navigationItem.title = "Schedule"
let ScheduleNavigationController = UINavigationController(rootViewController: scheduleController)
ScheduleNavigationController.title = "Schedule"
ScheduleNavigationController.tabBarItem.image = UIImage(named: "Calendar-50")

let settingsController = settingsViewController()
settingsController.navigationItem.title = "Settings"
let SettingsNavigationController = UINavigationController(rootViewController: settingsController)
SettingsNavigationController.title = "Settings"
SettingsNavigationController.tabBarItem.image = UIImage(named: "Settings-50")

viewControllers = [navigationController, MapNavigationController, ScheduleNavigationController, SettingsNavigationController]

tabBar.isTranslucent = true

let topBorder = CALayer()
topBorder.frame = CGRect(x: 0, y: 0, width: 1000, height: 0.5)
topBorder.backgroundColor = UIColor.white.cgColor
tabBar.layer.addSublayer(topBorder)
tabBar.clipsToBounds = true

}
}


AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()

window?.rootViewController = CustomTabBarController()

UINavigationBar.appearance().barTintColor = UIColor(red: 200/255, green: 0/255, blue: 0/255, alpha: 1)
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

UITabBar.appearance().tintColor = UIColor(red: 51/255, green: 90/255, blue: 149/255, alpha: 1)

application.statusBarStyle = .lightContent

// Override point for customization after application launch.
return true
}


I try and add a
UITextField
using:

var textField: UITextField


But when I launch my app the
textField
doesn't show up in the view.

Answer

Simply writing var textField: UITextField won't create a textField.

You need to initialize it, generally with a frame, and then add it to the view. It should look something like this:

var textField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, length: 100))

override func viewDidLoad(){
     super.viewDidLoad()
     self.view.addSubView(textField)
}

Just to test that you can visually see it easily as well, you could add something like this:

textField.backgroundColor = UIColor.blackColor()
Comments