如何在没有View Controller的情况下在swift上创建uitabbar项目

时间:2017-08-21 14:00:06

标签: ios swift uitabbarcontroller uitabbaritem

所以我的问题是我在没有故事板的情况下创建了一个UITabBarController,而且我仍然坚持如何在没有Viewcontroller的情况下创建UITabBar项目。因为我想要做的是我的UITabBarItem的第二项是不呈现视图控制器的动作。

3 个答案:

答案 0 :(得分:3)

我在应用程序(关闭按钮)中实现了类似的功能,尽管我使用的是故事板。关闭按钮是一个viewController,但我使用以下代码使其像常规按钮一样:

 func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    // this provides UI feedback that the button has been pressed, even though it leads to the dismissal
        if viewController == self.viewControllers![4] {

           viewController.tabBarItem.image? = UIImage(named: "TabBarClose")!.imageWithColor(UIColor.red).withRenderingMode(UIImageRenderingMode.alwaysOriginal)

            return false
        } else {
        return true
        }
    }

override func viewDidDisappear(_ animated: Bool) {
//sets the close button back to original image
    self.viewControllers![4].tabBarItem.image = UIImage(named: "TabBarClose")!
}

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// this is so it never actually goes to the close buttons viewController
    currentTab = self.selectedIndex != 4 ? self.selectedIndex:currentTab
    saveTabIndexToPreferences(currentTab!)

}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    // assign functionality to the close button
    if item.tag == 100 {// this is old code, there is probably a better way to reference the tab
        dismissTabBar()
    } else {

    }        
}

编辑(针对提出的问题)

func selectTabIndexFromPreferences() {
    let defaults:UserDefaults = UserDefaults.standard
    selectedIndex = defaults.integer(forKey: "songSelectionTabIndex_preference")
}

func saveTabIndexToPreferences(_ index:Int?) {
    if index != nil {
        let defaults:UserDefaults = UserDefaults.standard
        defaults.set(index!, forKey: "songSelectionTabIndex_preference")   
    }
}

tab-bar close button

答案 1 :(得分:1)

有一种符合API的方法可以做到这一点。

UITabBarControllerDelegateshouldSelect方法,您可以实施:https://developer.apple.com/documentation/uikit/uitabbarcontrollerdelegate/1621166-tabbarcontroller

这些方面的东西:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  if (viewController == self.centerViewController) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Test" message:@"This is a test" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
    [alert addAction:defaultAction];
    [tabBarController presentViewController:alert animated:YES completion:nil];

    return NO;
  }

  return YES;
}

答案 2 :(得分:0)

如果你真的想这样做(这是非常非标准的UI ...),那么你可以添加一个空的视图控制器,但是在你的标签栏中委托工具

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

}