代码之家  ›  专栏  ›  技术社区  ›  David Sanford Vivek

UISplitView的MasterViewController和导航问题

  •  6
  • David Sanford Vivek  · 技术社区  · 7 年前

    我正在更新我现有的应用程序,以包含iPad的SplitView。

    我使用的是UITabBar,但我的masterViewController出现了问题,因为它生成了一个“重复”导航栏,覆盖了所有masterViewController(选项卡)上的现有导航项目,包括搜索选项卡上的搜索栏。

    我的代码是:

    AppDelegate

    class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
    
        let splitViewController = self.window!.rootViewController as! UISplitViewController
        splitViewController.delegate = self
        splitViewController.preferredPrimaryColumnWidthFraction = 0.33
        splitViewController.minimumPrimaryColumnWidth = 375
        splitViewController.preferredDisplayMode = .allVisible
    
        return true
    }
    
    func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
        return true
    }
    

    在AppDelegate中使用此选项的原因是,我看到了一个示例,在该示例中,将其放置在hear将允许我不需要在每个不同的主视图(每个选项卡)中使用代码。由于仍在使用第一个主视图,因此尚未对此进行测试。

    主视图

    override func viewDidLoad()
    
    {
    
        self.extendedLayoutIncludesOpaqueBars = true
    
        self.navigationItem.hidesBackButton = true
    
        // 3D Touch
        if traitCollection.forceTouchCapability == .available {
            registerForPreviewing(with: self as UIViewControllerPreviewingDelegate, sourceView: view)
            ThreeDTouch = true
        }
    
        self.addSwitchVewButtonToNavigationBar()
        self.addCategoryButtonToNavigationBar()
    
    }
    
    func addSwitchVewButtonToNavigationBar() {
        let switchButton = UIButton(type: UIButtonType.custom)
    
        let editImage = UIImage(named: "CollectionButton")?.withRenderingMode(.alwaysTemplate)
        switchButton.setImage(editImage, for: .normal)
        switchButton.addTarget(self, action: #selector(SpeciesViewController.onSwitchView), for: UIControlEvents.touchUpInside)
        let switchButtonFinal = UIBarButtonItem(customView:switchButton)
    
        self.navigationItem.rightBarButtonItem = switchButtonFinal
    
    }
    
    @IBAction func onSwitchView(_ sender: UIBarButtonItem)
    {
        AppDelegate.getAppState().isListViewSelected = false
    
        let speciesColletion = storyboard?.instantiateViewController(withIdentifier: Resource.SpeciesCollectionStoryboard) as! SpeciesCollectionViewController
        self.navigationController?.viewControllers = [speciesColletion]
    }
    

    最初,onSwitchViewButton是使用IB嵌入的,但不起作用。这与详图视图上的addFavorite使用的系统相同。

    enter image description here

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  4
  •   MichaelV    7 年前

    您解决的导航控制器错误的问题。我假设在拆分master和SpeciesViewController之前,它们都存在于相同的导航环境中,并且使用相同的导航控制器。但在拆分视图中,它们并没有。您的详图控制器实际上是您正在寻找的导航控制器,它必须控制所有导航,并且必须有您需要的按钮。您可以通过以下方式从master获取:

    guard let split = splitViewController, let navController = split.viewControllers.last as? UINavigationController else { return }
    

    并确保您拆分的控制器未嵌入到另一个UINavigationController中(第二个导航栏的原因)。

    编辑: 返回详图导航控制器的函数:

    var detailsNavigationController: UINavigationController? {
        return splitViewController?.viewControllers.last as? UINavigationController
    }
    

    Storyboard Split 要访问blue call DetailsNavigationController,要访问red,请使用navigationController。