Swift 标签栏保持半透明

Swift Tab Bar remains translucent

提问人:tHatpart 提问时间:8/1/2023 更新时间:8/1/2023 访问量:20

问:

我正在尝试制作一个带有帖子按钮作为特色选项卡的“驼峰”标签栏控制器,但由于某种原因,无论我尝试什么,标签栏都保持半透明。当没有显示内容时,您可以看到它运行良好,但是一旦有内容,您可以看到选项卡栏和发布按钮容器的颜色差异。

如何使我的标签栏完全不透明?

enter image description here 在此处输入图像描述enter image description here

enter image description here

在我的MainTabBarViewController

func customizeTabBar() {
    postButtonContainer = UIView(frame: CGRect(x: 0, y: 10, width: tabBar.bounds.height + 30, height: tabBar.bounds.height + 30))
    postButtonContainer.center = CGPoint(x: tabBar.bounds.width / 2, y: tabBar.bounds.height / 3)
    postButtonContainer.backgroundColor = .white
    //UIColor.uStadium.takesLightestGray
    //tabBar.barTintColor = UIColor.uStadium.takesLightestGray
    
    postButtonContainer.layer.cornerRadius = postButtonContainer.frame.height / 2

    
    tabBar.addSubview(postButtonContainer)
    let bounds = CGRect(
        x: postButtonContainer.bounds.width / 10,
        y: postButtonContainer.bounds.height / 10,
        width: postButtonContainer.bounds.width / 1.25,
        height: postButtonContainer.bounds.height / 1.25
    )
    postButton = UIButton(frame: bounds)//postButtonContainer.bounds)
    postButton.setImage(UIImage(named:"create_button"), for: .normal)
    
    postButton.addTarget(self, action: #selector(showCompose), for: .touchUpInside)
    
    postButtonContainer.addSubview(postButton)
    
    let appearance2 = tabBar.standardAppearance
    appearance2.shadowImage = nil
    appearance2.shadowColor = nil
    self.tabBar.standardAppearance = appearance2
    // Set a custom background color for the tab bar
    tabBar.barTintColor = .white

    // Remove the default shadow from the tab bar
    tabBar.shadowImage = UIImage()

    // Set a custom shadow image for the tab bar
    tabBar.backgroundImage = UIImage(named: "custom_tabbar_background")

    // Set the tab bar item appearance to make the icons and text stand out
    let appearance = UITabBarItem.appearance()
    let attributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
    appearance.setTitleTextAttributes(attributes, for: .normal)
    let selectedAttributes = [NSAttributedString.Key.foregroundColor: UIColor.uStadium.green]
    appearance.setTitleTextAttributes(selectedAttributes, for: .selected)

    // Add shadow to the top of the tab bar
    tabBar.layer.shadowOffset = CGSize(width: 0, height: -3)
    tabBar.layer.shadowColor = UIColor.lightGray.cgColor
    tabBar.layer.shadowRadius = 5
    tabBar.layer.shadowOpacity = 0.5
    tabBar.isTranslucent = false
    tabBar.backgroundColor = .white
}
swift uikit uitabbarcontroller uitabbar

评论


答: 暂无答案