【发布时间】:2016-10-04 00:15:34
【问题描述】:
我正在开发一个 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 不会出现在视图中。
【问题讨论】:
标签: ios swift uitabbarcontroller uielement