Проблема перекрытия кнопки UITabbar NavigationBar
Я использовал программно для создания панели вкладок, мои взгляды похожи на
navigationController -> логин -> вкладка с кнопкой NavigationBar
First tab have : Left bar Image (button (logo), Right (notification icon, profile image)
Second tab have : Left bar button (Back), Right (notification icon, profile image)
third tab have : Left bar button (Back), Right (notification icon, profile image)
fourth tab have : Left bar button (Back), Right (notification icon, profile image)
Моя проблема:
Всякий раз, когда я пытаюсь перейти с помощью кнопки на первой вкладке (в ней UIButton), левая кнопка изображения на панели логотипа переходит на другую вкладку
код, используемый для перехода с одной вкладки на другую программу
@objc func jumptosecondTabbar(sender: UIButton){
self.tabBarController?.selectedIndex = 1
}
От открытия вкладки после входа в систему
let tabbar = self.storyboard?.instantiateViewController(withIdentifier: "DashboardTabBar") as! DashboardTabBar
tabbar.selectedIndex = 0
self.navigationController?.pushViewController(tabbar, animated: true)
Вот мой используемый класс вкладок:
import UIKit
class DashboardTabBar: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstVc = storyboard.instantiateViewController(withIdentifier: "HomeTabVC")
let firsticon = UITabBarItem(title: “First", image: UIImage(named: "ic_home_tab.png"), selectedImage: UIImage(named: "ic_home_tab.png"))
firstVc.tabBarItem = firsticon
let secondVC = storyboard.instantiateViewController(withIdentifier: "BookTabVC")
let secondicon = UITabBarItem(title: “Second", image: UIImage(named: "ic_book_tab.png"), selectedImage: UIImage(named: "ic_book_tab.png"))
secondVC.tabBarItem = secondicon
let thirdVC = storyboard.instantiateViewController(withIdentifier: "MyAppointTabVC")
let thirdicon = UITabBarItem(title: “Third", image: UIImage(named: "ic_myappt_tab.png"), selectedImage: UIImage(named: "ic_myappt_tab.png"))
thirdVC.tabBarItem = thirdicon
let fourthVC = storyboard.instantiateViewController(withIdentifier: "AccountTabVC")
let fourthicon = UITabBarItem(title: “Fourth", image: UIImage(named: "ic_account_tab.png"), selectedImage: UIImage(named: "ic_account_tab.png"))
fourthVC.tabBarItem = fourthicon
let controllers = [homeVc,bookVC,myApptVC,accountVC] //array of the root view controllers displayed by the tab bar interface
self.viewControllers = controllers
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//used to show tabfirstviewcontroller button
addLogoIcon()
addRightBarMenuItem()
}
func addLogoIcon()
{
// Add App Logo
navigationItem.hidesBackButton = true
let logobutton: UIButton = UIButton(type: UIButton.ButtonType.custom)
logobutton.setImage(UIImage(named: "ic_hw_logo.png"), for: .normal)
logobutton.setImage(UIImage(named: "ic_hw_logo.png"), for: .selected)
logobutton.setImage(UIImage(named: "ic_hw_logo.png"), for: .disabled)
logobutton.frame = CGRect(x: 0, y: 0, width: 127, height: 44)
logobutton.imageEdgeInsets = UIEdgeInsets(top: -2, left: -30, bottom: 0, right: 0);
let barlogoButton = UIBarButtonItem(customView: logobutton)
self.navigationItem.leftBarButtonItem = barlogoButton
self.navigationItem.leftBarButtonItem?.isEnabled = false
}
func addRightBarMenuItem()
{
//Add notification Button
let notificationImage = UIImage(named: "icon_notification")
let notificationbutton: UIButton = UIButton(type: UIButton.ButtonType.system)
notificationbutton.setImage(notificationImage, for: .normal)
notificationbutton.addTarget(self, action: #selector(buttonNotificationClicked(sender:)), for: UIControl.Event.touchUpInside)
notificationbutton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
notificationbutton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
let notificationBarButton = UIBarButtonItem(customView: notificationbutton)
notificationbutton.tintColor = .blue
// Add profile Picture
let profileImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
profileImageView.image = UIImage(named: "icon_male")
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
profileImageView.isUserInteractionEnabled = true
profileImageView.contentMode = .scaleToFill
profileImageView.addGestureRecognizer(tapGestureRecognizer)
profileImageView.image = imageProfile
profileImageView.layer.cornerRadius = 15
let profileBarButton = UIBarButtonItem(customView: profileImageView)
self.navigationItem.rightBarButtonItems = [ profileBarButton,notificationBarButton]
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
}
func addBackButton()
{
navigationItem.hidesBackButton = true
let logobutton: UIButton = UIButton(type: UIButton.ButtonType.custom)
logobutton.setImage(UIImage(named: "ic_back_arrow.png"), for: .normal)
logobutton.addTarget(self, action: #selector(buttonBackClicked(sender:)), for: UIControl.Event.touchUpInside)
logobutton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
logobutton.imageEdgeInsets = UIEdgeInsets(top: -2, left: -20, bottom: 0, right: 0);
let barlogoButton = UIBarButtonItem(customView: logobutton)
self.navigationItem.leftBarButtonItem = barlogoButton
}
@objc func buttonBackClicked(sender:UIButton) {
}
@objc func buttonProfileClicked(sender:UIButton) {
}
@objc func buttonNotificationClicked(sender:UIButton) {
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
guard let index = tabBar.items?.firstIndex(of: item) else { return }
switch index {
case 0:
addLogoIcon()
addRightBarMenuItem()
break
case 1:
addBackButton()
addRightBarMenuItem()
break
case 2:
addBackButton()
addRightBarMenuItem()
break
case 3:
addBackButton()
addRightBarMenuItem()
break
default:
break
}
}
}