Swift: Table in tabcontroller only is positioned correctly if it's the first tab?

Swift: Table in tabcontroller only is positioned correctly if it's the first tab?

我附上了说明我遇到的问题的示例代码。出于某种原因,tableview 需要成为第一个选项卡,以便 tableviewcontroller 识别导航栏。这个问题是对这个

的扩展

这里有一个 link 到实际的示例项目 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()
}

}

我通读了您的代码和 运行 您的项目,我认为问题在于您将 UITabBarController 嵌入到 UINavigationController 中。你想要做的是将每个 UIViewController(或 UITableViewController)嵌入到它自己的 UINavigationController 中,然后将这些导航控制器设置为 UITabBarController 的视图控制器(即你可以在故事板上设置它)。

这就是为什么第二个选项卡上的 tableviewcontroller 的内容插入也有问题(第一行隐藏在导航栏下)。嵌入了 UITabBarController 的 UINavigationController 没有通过 UITabBarController 将正确的插入传递到它的视图控制器。

我对你的项目做了一些修改:TableTest


Main.storyboard

我在自己的 UINavigationController 中嵌入了每个 UITabBarController 的视图控制器


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