如何使用Swift从UITabBarController的子类中触发动作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Swift从UITabBarController的子类中触发动作相关的知识,希望对你有一定的参考价值。

我在ios应用程序上创建了UITabBarController的子类,因为我想在标签栏上添加中间的“ +”按钮。我使用下面的代码添加了这个额外的按钮:

class XGTabBarViewController: UITabBarController 

let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
private let optionMenu = UIAlertController(title: nil, message: "New", preferredStyle: .actionSheet)

override func viewDidLoad() 
    super.viewDidLoad()

    let action1 = UIAlertAction(title: "Action 1", style: .default)
    let action2 = UIAlertAction(title: "Action 2", style: .default)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)

    optionMenu.addAction(action1)
    optionMenu.addAction(action2)
    optionMenu.addAction(cancelAction)

    var menuButtonFrame = menuButton.frame
    let iconConfig = UIImage.SymbolConfiguration(pointSize: 24, weight: .medium, scale: .medium)

    let iconImage = UIImage(systemName: "plus", withConfiguration: iconConfig)
    let icon = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
    icon.image = iconImage
    icon.tintColor = UIColor.white

    menuButtonFrame.origin.y = view.bounds.height - menuButtonFrame.height - 50
    menuButtonFrame.origin.x = view.bounds.width/2 - menuButtonFrame.size.width/2
    menuButton.frame = menuButtonFrame

    menuButton.backgroundColor = UIColor(named: "LinkBlue")
    menuButton.layer.cornerRadius = menuButtonFrame.height/2
    menuButton.layer.shadowColor = UIColor.black.cgColor
    menuButton.layer.shadowRadius = 3
    menuButton.layer.shadowOpacity = 0.3
    menuButton.layer.shadowOffset = CGSize(width: 0, height: 3)

    menuButton.addSubview(icon)
    icon.translatesAutoresizingMaskIntoConstraints = false
    icon.widthAnchor.constraint(equalToConstant: 30).isActive = true
    icon.heightAnchor.constraint(equalToConstant: 30).isActive = true
    icon.centerXAnchor.constraint(equalTo: menuButton.centerXAnchor).isActive = true
    icon.centerYAnchor.constraint(equalTo: menuButton.centerYAnchor).isActive = true

    view.addSubview(menuButton)

    menuButton.addTarget(self, action: #selector(menuButtonAction(sender:)), for: .touchUpInside)

    view.layoutIfNeeded()


@objc private func menuButtonAction(sender: UIButton) 
    self.present(optionMenu, animated: true, completion: nil)

我通过IB设置了标签栏控制器。这就是它的样子

enter image description here

所以,我想知道是否应该在此类中定义菜单按钮操作?还是有更好的(最佳实践)方法?

如您所见,我试图添加一个操作表,该操作表将在点击按钮时显示。但是我遇到了一些布局错误。我想知道是否在正确的位置添加了此代码。

我收到以下错误:

enter image description here

谢谢!

答案

请添加标签栏控制器,并将三个viewcontroller连接到标签栏。中心viewcontroller标签栏iteam设置图像插图。

无布局错误。

Tabbar image

Tabbar image inset

import UIKit

class TabBarVC: UITabBarController 

override func viewDidLoad() 
    super.viewDidLoad()


    let tabBar = self.tabBar
    (tabBar.items![0] ).selectedImage = UIImage(named:"icn_audio_icon")?.withRenderingMode(.alwaysOriginal)
    (tabBar.items![1] ).selectedImage = UIImage(named:"icn_home_active")?.withRenderingMode(.alwaysOriginal)
    tabBar.items![2].image = UIImage(named:"icn_categary")?.withRenderingMode(.alwaysOriginal)

    tabBar.items![0].image = UIImage(named:"icn_audio_icon")?.withRenderingMode(.alwaysOriginal)
    tabBar.items![1].image = UIImage(named:"icn_home")?.withRenderingMode(.alwaysOriginal)
    tabBar.items![2].image = UIImage(named:"icn_categary")?.withRenderingMode(.alwaysOriginal)

    self.tabBarController?.selectedIndex = 1
    tabBar.layer.shadowColor = UIColor.red.cgColor
    tabBar.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
    tabBar.layer.shadowRadius = 5
    tabBar.layer.shadowOpacity = 0.6
    tabBar.layer.masksToBounds = false



  override func viewDidLayoutSubviews() 
     super.viewDidLayoutSubviews()
     self.tabBar.invalidateIntrinsicContentSize()
  



func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool 
  if viewController.isKind(of: ActionViewController.self) 
     let vc =  ActionViewController()
     vc.modalPresentationStyle = .overFullScreen
     self.present(vc, animated: true, completion: nil)
     return false
  
  return true

以上是关于如何使用Swift从UITabBarController的子类中触发动作的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 swift 从 iOS 设备上传文件(到 REDCap)

如何使用 Emscripten 将 Hello Word 从 Swift 编译为 JavaScript

如何使用 Swift 从 iOS HealthKit 应用程序读取心率?

如何使用 Swift 从 iOS HealthKit 应用程序读取心率?

如何使用swift从ios应用程序调用苹果钱包

如何使用 Swift 从 tableview 和 sqlite3 中删除