不在视图控制器中显示的UI元素是自定义标签栏控制器的一部分



我正在开发一个iOS应用程序,该应用程序有4个以上的选项卡,用户需要能够访问。我决定创建自己的自定义标签栏控制器,这样我就有更多的自定义选项。我已经实现了自定义标签栏控制器,并且能够在视图之间切换,除了当我在视图控制器类中添加UI元素(如UITextField, UIButton等)并运行应用程序时,UI元素不显示。

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
    }

我尝试添加一个UITextField使用:

var textField: UITextField

但是当我启动我的应用程序的textField不显示在视图中

简单地写var textField: UITextField不会创建textField。

你需要初始化它,通常用一个框架,然后将它添加到视图。它应该看起来像这样:

var textField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, length: 100))
override func viewDidLoad(){
     super.viewDidLoad()
     self.view.addSubView(textField)
}

只是为了测试你可以很容易地看到它,你可以添加这样的东西:

textField.backgroundColor = UIColor.blackColor()

最新更新