我附上了示例代码,说明了我遇到的问题。由于某种原因,tableview需要是第一个选项卡,以便tableviewcontroller识别导航栏。此问题是此question I asked yesterday
的扩展以下是实际示例项目iosTableTabTest
的链接的AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let containerViewController:ContainerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()
return true
}
ContainerViewController.swift:
class ContainerViewController: UIViewController, LoginDelegate {
var mainNavigationController: UINavigationController!
var myTabBarController: UITabBarController!
var loginViewController: LoginViewController!
override func viewDidLoad() {
super.viewDidLoad()
var mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
myTabBarController = mainStoryboard.instantiateViewControllerWithIdentifier("myTabBarController") as? UITabBarController
loginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginViewController") as? LoginViewController
loginViewController.delegate = self
mainNavigationController = UINavigationController(rootViewController: loginViewController)
view.addSubview(mainNavigationController.view)
addChildViewController(mainNavigationController)
mainNavigationController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func loginPressed() {
self.mainNavigationController.pushViewController(self.myTabBarController, animated: false)
}
}
LoginViewController.swift:
@objc
protocol LoginDelegate{
func loginPressed()
}
class LoginViewController: UIViewController {
@IBOutlet var loginBtn: UIButton!
var delegate: LoginDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func loginPressed(sender: AnyObject) {
delegate?.loginPressed()
}
}
FirstTabViewController.swift:
class FirstTabViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.topViewController.navigationItem.setHidesBackButton(true, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
SecondTabViewController.swift:
class SecondTabViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:0)
我阅读了您的代码并运行了您的项目,我相信问题在于您将UITabBarController嵌入到UINavigationController中。你想要做的是将每个UIViewController(或UITableViewController)嵌入到自己的UINavigationController中,然后将这些导航控制器设置为UITabBarController的视图控制器(即你可以在故事板上设置它)。
这就是为什么第二个选项卡(隐藏在导航栏下面的第一行)中tableviewcontroller的内容插入也存在问题。嵌入了UITabBarController的UINavigationController不会通过UITabBarController将适当的insets传递给它的视图控制器。
我对您的项目进行了一些更改:TableTest
<强> Main.storyboard 强>
我在每个UITabBarController的视图控制器中嵌入了自己的UINavigationController
<强> ContainerViewController 强>
我添加了loginViewController和myTabBarController的视图作为容器VC视图的子视图,然后将每个控制器添加为容器VC的子控制器。如果要使用导航栏,仍然可以将loginViewController嵌入到UINavigationController中,但为了简单起见,我将其删除了。
class ContainerViewController: UIViewController, LoginDelegate {
//var mainNavigationController: UINavigationController!
var myTabBarController: UITabBarController!
var loginViewController: LoginViewController!
override func viewDidLoad() {
super.viewDidLoad()
var mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
myTabBarController = mainStoryboard.instantiateViewControllerWithIdentifier("myTabBarController") as? UITabBarController
loginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginViewController") as? LoginViewController
loginViewController.delegate = self
//mainNavigationController = UINavigationController(rootViewController: loginViewController)
//view.addSubview(mainNavigationController.view)
//addChildViewController(mainNavigationController)
//mainNavigationController.didMoveToParentViewController(self)
view.addSubview(myTabBarController.view)
view.addSubview(loginViewController.view)
self.addChildViewController(myTabBarController)
self.addChildViewController(loginViewController)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func loginPressed() {
//self.mainNavigationController.pushViewController(self.myTabBarController, animated: false)
loginViewController.view.removeFromSuperview()
loginViewController.removeFromParentViewController()
}
}
<强> FirstTabViewController 强>
由于此视图控制器现在嵌入在故事板中自己的UINavigationController中,因此它可以像self.navigationItem一样访问其navigationItem
class FirstTabViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//self.navigationController?.topViewController.navigationItem.setHidesBackButton(true, animated: true)
self.navigationItem.setHidesBackButton(true, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}