UI 元素未显示在与自定义标签栏控制器分开的视图控制器中
UI elements not displaying in view controllers that are apart of custom tab bar controller
我正在开发一个 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
不会创建文本字段。
需要初始化,一般用frame,然后添加到view中。它应该看起来像这样:
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()
我正在开发一个 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
不会创建文本字段。
需要初始化,一般用frame,然后添加到view中。它应该看起来像这样:
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()