为现有的视图控制器创建标签栏?

时间:2018-06-14 03:36:32

标签: ios swift uiviewcontroller uitabbarcontroller

我一直在编写没有故事板的swift。通过这样做,我认为将标签栏添加到我的视图控制器会很有趣。我一直在研究尝试这种方法的不同方法,但是,我还没有找到适合我的应用的答案。

    //This is my app delegate

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    let mainController = UINavigationController(rootViewController:       mainViewController())
    let loginController = UINavigationController(rootViewController: LoginController())

    FirebaseApp.configure()
    userIsloggedIn = defaults.bool(forKey: "User is Logged In")
    if userIsloggedIn == true {
      window?.rootViewController = mainController
    } else {
       window?.rootViewController = loginController
    }
    UINavigationBar.appearance().barTintColor = GREEN_Theme
    return true
}

//这是我的TabBar控制器,放在我的“mainViewController”

class CustomTabBarController:UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    self.tabBar.barTintColor = GREEN_Theme
    self.tabBar.isTranslucent = true
    self.tabBar.tintColor = .white

    let mainController = UINavigationController(rootViewController: mainViewController())
    mainController.tabBarItem.title = "Find Jobs"
    mainController.tabBarItem.image = UIImage(named: "job-search")
    let settingsController = UINavigationController(rootViewController: settingsViewController())
    settingsController.tabBarItem.title = "Settings"
    settingsController.tabBarItem.image = UIImage(named: "settings")
    let notificationsController = UINavigationController(rootViewController: notificationsViewController())
    notificationsController.tabBarItem.title = "Notifications"
    notificationsController.tabBarItem.image = UIImage(named: "notification")
    let messagesController = UINavigationController(rootViewController: messagesViewController())
    messagesController.tabBarItem.title = "Messages"
    messagesController.tabBarItem.image = UIImage(named: "chat")
    let historyController = UINavigationController(rootViewController: jobhistoryViewController())
    historyController.tabBarItem.title = "Job History"
    historyController.tabBarItem.image = UIImage(named: "medical-history")


    viewControllers = [historyController, notificationsController, mainController, messagesController, settingsController]
    return

}
}

//最后,这是我的mainViewController

import UIKit

导入基金会 导入Firebase 导入MapKit 导入GoogleMaps 导入CoreLocation

class mainViewController:UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

private var locationManager = CLLocationManager()
var mapView = GMSMapView()
var camera = GMSCameraPosition()
var sidebarView: SidebarView!
var blackScreen: UIView!

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    let lastLocation = locations[locations.count-1]
    if lastLocation.horizontalAccuracy>0{
        locationManager.stopUpdatingLocation()
        let latitude = lastLocation.coordinate.latitude
        let longitude = lastLocation.coordinate.longitude

    }
}

override func viewDidLoad() {
    super.viewDidLoad()



    let btnMenu = UIBarButtonItem(image: #imageLiteral(resourceName: "menu-button-of-three-horizontal-lines"), style: .plain, target: self, action: #selector(btnMenuAction))
    btnMenu.tintColor=UIColor(red: 54/255, green: 55/255, blue: 56/255, alpha: 1.0)
    self.navigationItem.leftBarButtonItem = btnMenu

    sidebarView=SidebarView(frame: CGRect(x: 0, y: 0, width: 0, height: self.view.frame.height))
    sidebarView.delegate=self
    sidebarView.layer.zPosition=100
    self.view.isUserInteractionEnabled=true
    self.navigationController?.view.addSubview(sidebarView)

    blackScreen=UIView(frame: self.view.bounds)
    blackScreen.backgroundColor=UIColor(white: 0, alpha: 0.5)
    blackScreen.isHidden=true
    self.navigationController?.view.addSubview(blackScreen)
    blackScreen.layer.zPosition=99
    let tapGestRecognizer = UITapGestureRecognizer(target: self, action: #selector(blackScreenTapAction(sender:)))
    blackScreen.addGestureRecognizer(tapGestRecognizer)
}

@objc func btnMenuAction() {
    blackScreen.isHidden=false
    UIView.animate(withDuration: 0.3, animations: {
        self.sidebarView.frame=CGRect(x: 0, y: 0, width: 250, height: self.sidebarView.frame.height)
    }) { (complete) in
        self.blackScreen.frame=CGRect(x: self.sidebarView.frame.width, y: 0, width: self.view.frame.width-self.sidebarView.frame.width, height: self.view.bounds.height+100)
    }
}

@objc func blackScreenTapAction(sender: UITapGestureRecognizer) {
    blackScreen.isHidden=true
    blackScreen.frame=self.view.bounds
    UIView.animate(withDuration: 0.3) {
        self.sidebarView.frame=CGRect(x: 0, y: 0, width: 0, height: self.sidebarView.frame.height)
    }
}

}

extension mainViewController: SidebarViewDelegate {
    func sidebarDidSelectRow(row: Row) {
        blackScreen.isHidden=true
        blackScreen.frame=self.view.bounds
        UIView.animate(withDuration: 0.3) {
            self.sidebarView.frame=CGRect(x: 0, y: 0, width: 0, height: self.sidebarView.frame.height)
        }
        switch row {
        case .editProfile:
            let vc=EditProfileVC()
            self.navigationController?.pushViewController(vc, animated: true)
        case .reviews:
            print("Reviews")
        case .contact:
            print("Contact")
        case .payment:
            print("Payment")
        case .share:
            print("Share")
        case .help:
            print("Help")
        case .signOut:
            print("Sign out")
            let SignOutvc=signoutVC()
            self.navigationController?.pushViewController(SignOutvc, animated: true)
        case .none:
            break
        }
        GMSServices.provideAPIKey("AIzaSyBDOLisA3c-wDTbkbSssAxEb3iLw7Y5vHo")
        let camera = GMSCameraPosition.camera(withLatitude: 40.001850, longitude: -83.019405, zoom: 17)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        self.view = mapView
        mapView.isMyLocationEnabled = true



    let currentLocation = CLLocationCoordinate2DMake(40.001850, -83.019405)
    let marker = GMSMarker(position: currentLocation)
    marker.snippet = "Current Location"
    marker.map = mapView
    self.mapView.addSubview(mapView)


    func getLocation(){
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

    }

    navigationController?.navigationBar.prefersLargeTitles = false
    navigationItem.title = "Welcome To Odd Jobs"
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 25)]
}
        func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
            reverseGeocodeCoordinate(position.target) // sending data when the mapView is not moved or pinched by the finger //
        }

        func reverseGeocodeCoordinate(_ coordinate: CLLocationCoordinate2D) {
            let geocoder = GMSGeocoder()
            geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
                guard let address = response?.firstResult(), let _ = address.lines else {
                    return
    }
   }
  }
}

我的目标是将customTabBarViewController与我的主View控制器结合使用。如果我运行我的应用程序,它将我设置为customTabBar,它与我的mainVC重叠。如果我退出并尝试登录,我将被置于黑色背景的默认tabBar屏幕。

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您的问题是什么。您说由于您的登录,您不希望将UITabBarController添加为rootViewController,但是在您的示例中,您提供的解决方案是根据登录情况调整要设置的视图控制器。(在登录后再次调用该条件。)

但是,要直接回答您的问题,如果您要将UITabBarController添加到UIViewController,则应使用遏制。

// Adding a contained view controller
addChildViewController(tabBarController)
view.addSubview(tabBarController.view)
tabBarController.didMove(toParentViewController: self)

// Removing a contained view controller
tabBarController.willMove(toParentViewController: nil)
tabBarController.view.removeFromSuperview()
tabBarController.removeFromParentViewController()