TabBar tappedIcon unTappedIcon问题

时间:2018-02-20 09:16:50

标签: ios swift3 uitabbar xcode9

当我点击我的TabBar的第一张图片时,它应该像下图一样突出显示 - enter image description here

但是当我点击第一个图标时它没有任何效果,点击第二个图标突出显示第一个图标,点击第三个图标突出显示它的上一个图标(第二个图标),依此类推。这可能是一个索引问题。需要一些帮助才能找到这个问题。谢谢。我的TabBar当前视图 - enter image description here

这是我的代码 -

import UIKit

class ViewController: BaseViewController {
    //MARK: Outlets
    @IBOutlet var warningsIcon: UIImageView!
    @IBOutlet var incidentsIcon: UIImageView!
    @IBOutlet var notificationsIcon: UIImageView!
    @IBOutlet var myMessagesIcon: UIImageView!
    @IBOutlet var warningsTitle: UILabel!
    @IBOutlet var incidentsTitle: UILabel!
    @IBOutlet var notificationsTitle: UILabel!
    @IBOutlet var myMessagesTitle: UILabel!
    @IBOutlet var containerView: UIView!

    //MARK: PROPERTIES
     var previousSelectedIcon:UIImageView!
     var previousSeelctedTitle:UILabel!
     var previousIndex:Int!
     var currentIndex:Int!
     var previousVc:UIViewController!
    fileprivate var i = 0
    open var subViewController:UIViewController?
    var unTappedIcon = ["warnings","incidents","notifications","my-messages"]
    var tappedIcon = ["warnings_tapped","Incidents_tapped","notifications_tapped","myMessages_tapped"]


    override func viewDidLoad() {
        super.viewDidLoad()
        addSlideMenuButton()
        // Do any additional setup after loading the view, typically from a nib.

        previousVc = self;

        previousSelectedIcon = warningsIcon
        previousSeelctedTitle = warningsTitle

        previousIndex = 0;
        currentIndex = 0;

        //place sub view controller if any
        placeSubViewControllerIfNeeded()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //place sub view controller if needed
    func placeSubViewControllerIfNeeded() -> Void{
        if let vc = subViewController {
            vc.view.frame = self.containerView.frame
            self.addChildViewController(vc)
            //vc.view.frame = self.view.frame
            self.containerView.addSubview(vc.view)
            self.view.sendSubview(toBack: vc.view)
        }
    }


    //MARK: For Tab bar
     func updateTapView(title:UILabel, icon:UIImageView) {

        previousSeelctedTitle.textColor = UIColor(red:0.60, green:0.60, blue:0.60, alpha:1.0)
        previousSelectedIcon.image = UIImage.init(named: unTappedIcon[previousIndex])

        title.textColor = UIColor(red:0.26, green:0.18, blue:0.49, alpha:1.0)
        icon.image = UIImage.init(named: tappedIcon[currentIndex])
    }

    func removePreviousViewController() {
        previousVc.willMove(toParentViewController: nil)
        previousVc.view.removeFromSuperview()
        previousVc.removeFromParentViewController()
    }

    func getStoryBoardByIndentifier(identifier:String)->UIStoryboard {
        return  UIStoryboard.init(name: identifier, bundle: nil)
    }

    func showSubViewContrller(subViewController:UIViewController) {

        self.addChildViewController(subViewController)

        subViewController.view.frame = containerView.frame
        self.containerView.addSubview(subViewController.view)

        subViewController.didMove(toParentViewController: self)
        previousVc = subViewController
    }



    //Mark:- Action button for warnings
    @IBAction func onClickWarnings(_ sender: UITapGestureRecognizer) {
        print("Hi there i am just a warning")

        self.warningsIcon.image = UIImage.init(named: "warnings_tapped")
        previousIndex = currentIndex
        currentIndex = 0
        updateTapView( title: warningsTitle, icon: warningsIcon)

        previousSeelctedTitle = warningsTitle
        previousSelectedIcon = warningsIcon

        if i > 0{
            removePreviousViewController()
            print("i am already removed")
        }

        let storyBoard = getStoryBoardByIndentifier(identifier: "warnings")
        let vc = storyBoard.instantiateViewController(withIdentifier: "WarningsViewController") as! WarningsViewController
        showSubViewContrller(subViewController: vc)

        i += 1
    }

    //Mark:- Action button for warnings
    @IBAction func onClickIncidents(_ sender: UITapGestureRecognizer) {
        print("Hi there i am just a warning")

        self.incidentsIcon.image = UIImage.init(named: "Incidents_tapped")
        previousIndex = currentIndex
        currentIndex = 1
        updateTapView( title: incidentsTitle, icon: incidentsIcon)

        previousSeelctedTitle = incidentsTitle
        previousSelectedIcon = incidentsIcon

        if i > 0{
            removePreviousViewController()
            print("i am already removed")
        }


        let storyBoard = getStoryBoardByIndentifier(identifier: "incidents")
        let vc = storyBoard.instantiateViewController(withIdentifier: "IncidentsViewController") as! IncidentsViewController
        showSubViewContrller(subViewController: vc)

        i += 1

    }

    //Mark:- Action button for warnings
    @IBAction func onClickNotifications(_ sender: UITapGestureRecognizer) {
        print("Really true")
      self.notificationsIcon.image = UIImage.init(named: "notifications_tapped")
        previousIndex = currentIndex
        currentIndex = 2
        updateTapView(title: notificationsTitle, icon: notificationsIcon)

        previousSeelctedTitle = notificationsTitle
        previousSelectedIcon = notificationsIcon

        if i > 0 {
            removePreviousViewController()
            print("I am already removed")
        }

        let storyBoard = getStoryBoardByIndentifier(identifier: "notifications")
        let vc = storyBoard.instantiateViewController(withIdentifier: "NotificationsViewController") as! NotificationsViewController
        showSubViewContrller(subViewController: vc)

        i += 1
    }

    //Mark:- Action button for warnings
    @IBAction func onClickMyMessages(_ sender: UITapGestureRecognizer) {
        print("ha ha..i believe in that")
        self.myMessagesIcon.image = UIImage.init(named: "myMessages_tapped")
        previousIndex = currentIndex
        currentIndex = 3
        updateTapView(title: myMessagesTitle , icon: myMessagesIcon)

        previousSeelctedTitle = myMessagesTitle
        previousSelectedIcon = myMessagesIcon

        if i > 0 {
            removePreviousViewController()
            print("i am alreday removed")
        }

        let storyBoard = getStoryBoardByIndentifier(identifier: "myMessages")
        let vc = storyBoard.instantiateViewController(withIdentifier: "MyMessagesViewController") as! MyMessagesViewController
        showSubViewContrller(subViewController: vc)

        i += 1
    }

}

1 个答案:

答案 0 :(得分:0)

看起来你正在添加你想要显示的视图,如同一个标签栏项目...这可能不是正确的方法..你可以做的是制作自定义标签栏控制器

class SampleViewController: UITabBarController {

@IBOutlet var tabBarView: UIView!
@IBOutlet weak var item1Btn: UIButton!
@IBOutlet weak var item2Btn: UIButton!
@IBOutlet weak var item3Btn: UIButton!
@IBOutlet weak var item4Btn: UIButton!
@IBOutlet weak var item5Btn: UIButton!


let item1Image = #imageLiteral(resourceName: "profileUnselected")
let item1Selected = #imageLiteral(resourceName: "profile")
let item2Image = #imageLiteral(resourceName: "notificationUnselected")
let item2Selected = #imageLiteral(resourceName: "notificationSelected")
let item3Image = #imageLiteral(resourceName: "game")
let item3Selected = #imageLiteral(resourceName: "game")
let item4Image = #imageLiteral(resourceName: "cookbooksUnselected")
let item4Selected = #imageLiteral(resourceName: "cookbooks")
let item5Image = #imageLiteral(resourceName: "searchTabUnselected")
let item5Selected = #imageLiteral(resourceName: "searchTabSelected")

let unSelectedColor = UIColor.lightGray
let selectedColor = UIColor.white
let appColor = UIColor(red: 22.0/255.0, green: 168.0/255.0, blue: 225.0/255.0, alpha: 1.0)

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    if UIDevice().userInterfaceIdiom == .phone {
        switch UIScreen.main.nativeBounds.height {
        case 2436:
            offsetHeight = 44.0
        default:
            print("unknown")
        }
    }

    addTabView()
}

func addTabView() {

    Bundle.main.loadNibNamed("TabBarView", owner: self, options: nil)


    tabBarView.frame = CGRect(x:
        0 , y: Helper.getScreenHeight() - AppConstants.kTabBarheight - offsetHeight, width: Helper.getScreenWidth(), height: AppConstants.kTabBarheight)
    self.view.addSubview(tabBarView)


    self.item3Btn.sendActions(for: .touchUpInside) //here i showed my third tab bar item initially


}

@IBAction func tabBarBtnClick(_ sender: UIButton) {

    var isViewToChange = false
    let currentIndex = self.selectedIndex
    var tochangeIndex = sender.tag


    self.item1Btn.setImage(item1Image, for: .normal)
    self.item2Btn.setImage(item2Image, for: .normal)
    self.item3Btn.setImage(item3Image, for: .normal)
    self.item4Btn.setImage(item4Image, for: .normal)
    self.item5Btn.setImage(item5Image, for: .normal)




    switch sender.tag {

    case 0:

        self.item1Btn.setImage(item1Selected, for: .normal)

        if currentIndex != 0{
            isViewToChange = true
            tochangeIndex = 0
        }
        else{
            (self.viewControllers?[currentIndex] as! UINavigationController).popToRootViewController(animated: true)
        }


    case 1:

        self.item2Btn.setImage(item2Selected, for: .normal)

        if currentIndex != 1{
            isViewToChange = true
            tochangeIndex = 1

        }
        else{
            (self.viewControllers?[currentIndex] as! UINavigationController).popToRootViewController(animated: true)
        }

    case 2:



        if currentIndex != 2{

            isViewToChange = true
            tochangeIndex = 2

        }
        else{
            (self.viewControllers?[currentIndex] as! UINavigationController).popToRootViewController(animated: true)
        }

    case 3:
        self.item4Btn.setImage(item4Selected, for: .normal)

        if currentIndex != 3{

            isViewToChange = true
            tochangeIndex = 3

        }
        else{
            (self.viewControllers?[currentIndex] as! UINavigationController).popToRootViewController(animated: true)
        }

    case 4:


        self.item5Btn.setImage(item5Selected, for: .normal)
        if currentIndex != 4{

            isViewToChange = true
            tochangeIndex = 4

        }
        else{
            (self.viewControllers?[currentIndex] as! UINavigationController).popToRootViewController(animated: true)
        }


    default:
        break

    }

    if isViewToChange {

        let _ = self.selectedViewController?.view
        let _ = self.viewControllers?[tochangeIndex].view
        self.selectedIndex = tochangeIndex

    }
}


func hideTabbar() {

    self.tabBar.isHidden = true
    var frame = tabBarView.frame
    frame.origin.y = Helper.getScreenHeight() + 200
    tabBarView.frame = frame

}

func showTabbar() {
    self.tabBar.isHidden = false
    var frame = tabBarView.frame
    frame.origin.y = Helper.getScreenHeight() - AppConstants.kTabBarheight -  offsetHeight
    tabBarView.frame = frame

}

}

这里我有一些方法让你在我们推入每个标签栏项目的堆栈时显示隐藏标签栏。

这里添加了一些图片供您理解

this is the interfacebuilder where i have added a tab bar controller and its child view controller

this is custom tab view image

并且也不要忘记在标签栏视图中为每个按钮设置标签,这样当点击任何按钮时,会调用相同的方法并从那里处理它