中间凸起的tabbar ios

iOS13以上才行

 

import UIKit
 
class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTabBarAppearance()
    addCenterButton()

    }
 
    private func setupTabBarAppearance() {
        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = .white
        tabBar.standardAppearance = appearance
        tabBar.scrollEdgeAppearance = appearance
    }

private func addCenterButton() {
    let button = UIButton(type: .system)
    button.setImage(UIImage(systemName: "plus"), for: .normal) // 使用系统图标作为示例
    button.tintColor = .blue // 设置按钮颜色
    button.addTarget(self, action: #selector(centerButtonTapped), for: .touchUpInside)
    button.frame = CGRect(x: 0, y: 0, width: 60, height: 60) // 设置按钮大小和位置
    button.center = tabBar.center // 将按钮置于tab bar中心
    button.layer.zPosition = 1 // 确保按钮在tab bar之上
    tabBar.addSubview(button) // 将按钮添加到tab bar上
    tabBar.bringSubviewToFront(button) // 确保按钮在最前面
}
 
@objc private func centerButtonTapped() {
    print("Center button tapped")
}

}

 

posted @ 2025-09-12 10:08  黄增松  阅读(14)  评论(0)    收藏  举报