我从storyboard添加了2个tabBar项目,并以编程方式添加了一个UITabBarItem - Menu 。我成功地打开了使用storyboard创建的tabBarItems对应的控制器。但是,当我点击"菜单"出现一个空白的黑屏,
@objc public class MainScreenTabsController : UITabBarController {
public override func viewDidLoad() {
super.viewDidLoad()
let tabController = MyViewController()
let tabBarItem = UITabBarItem(title: "Menu", image: UIImage(named: "more-options.png"), selectedImage: UIImage(named: "more-options"))
tabController.tabBarItem = tabBarItem
var array = self.viewControllers
array?.append(tabController)
self.viewControllers = array
}
public func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return true;
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}
我按照几个教程添加了标签栏项,但所有这些都有我写的代码。我错过了一些非常基本的东西吗?
编辑:
菜单控制器类
@objc public class MyViewController:UIViewController {
public override func viewDidLoad() {
super.viewDidLoad()
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}
答案 0 :(得分:3)
您的应用正在执行您的代码告诉它要执行的操作。您正在创建MyViewController
的实例,并将其添加到UITabBarController
的视图控制器数组中。
您的MyViewController
类文件只是定义了一个空白的黑色视图。
我猜你在故事板中创建了一个想要用作MyViewController
的ViewController?如果是这样,您需要从故事板中实例化。
如果您正在编辑故事板,请将MyViewController
类分配给要使用的VC,也为其指定Storyboard ID
- 例如{ {1}}。然后,将MyVC
功能编辑为:
viewDidLoad
答案 1 :(得分:0)
因为你是以编程方式创建ViewController,即。如果没有nib / storyboard,您负责实例化UIView对象并设置视图控制器的view属性。为此,实现loadView方法并将视图对象分配给viewController的view属性。然后你可以为视图对象添加自定义视图,检查下面的代码。
class MyViewController: UIViewController {
override func loadView() {
// super.loadView() // DO NOT CALL SUPER
//create view
view = UIView()
view.backgroundColor = UIColor.white
//Add a custom view with red color
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = UIColor.red
view.addSubview(customView)
NSLayoutConstraint.activate(
[customView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
customView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
customView.topAnchor.constraint(equalTo: view.topAnchor),
customView.bottomAnchor.constraint(equalTo: view.bottomAnchor)]
)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
最好使用Storyboard / Nib来实现此目的,因为您可以使用界面构建器中的自动布局轻松配置自定义视图/控件,而不是以编程方式执行。 :)
修改强>
如果您使用故事板然后实例化视图控制器,如下面的代码所示
class MainScreenTabsController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let tabController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController") as! MainViewController //if using storyboard
let icon = UITabBarItem(title: "Menu", UIImage(named: "more-options.png"), selectedImage: UIImage(named: "more-options")))
tabController.tabBarItem = icon
var controllers = self.viewControllers
controllers?.append(tabController)
self.setViewControllers(controllers!, animated: true)
}
}