【问题标题】:UI elements not displaying in view controllers that are apart of custom tab bar controllerUI 元素未显示在自定义选项卡栏控制器之外的视图控制器中
【发布时间】:2016-10-04 00:15:34
【问题描述】:

我正在开发一个 iOS 应用程序,该应用程序还有 4 个用户需要能够访问的选项卡。我决定创建自己的自定义标签栏控制器,以便在自定义方面有更多选择。我已经实现了自定义标签栏控制器,并且能够很好地在视图之间切换,除非我必须在我的视图控制器类中添加 UI 元素(例如 UITextFieldUIButton 等)并运行 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


    【解决方案1】:

    只写var textField: UITextField 不会创建文本字段。

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

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      相关资源
      最近更新 更多