【问题标题】:Adding Corner To UITabBarController将角添加到 UITabBarController
【发布时间】:2020-12-05 20:58:56
【问题描述】:

您好,我是 iOS 开发新手,遇到了我正在努力克服的挑战。基本上我想将角半径添加到 UITabBarController,但我不知道如何。我检查了 StackoverflowYoutube 中的几个资源,人们共享的解决方案要么太复杂而无法理解,要么给出错误,例如 "Value of type 'UITabBarController ' 没有成员 'layer'。”。顺便说一句,我不使用情节提要。

(注意:代码中的 cmets 用于样式设置。由于无法使它们工作,因此我对其进行了注释。)

代码:

import UIKit

class TabBarViewController: UIViewController, UITabBarControllerDelegate {
    
    var tabBarCnt: UITabBarController!
    override func viewDidLoad() {
        super.viewDidLoad()
        createTabBar()
        setUpBarStyle()
    }
    func createTabBar(){
        //        viewcontroller.tabBarItem.title = "Dash"
        //        viewcontroller.tabBarItem.image = UIImage.init(named: "imageName")
        //        viewcontroller.tabBarItem.selectedImage= UIImage.init(named: "imageName")
        tabBarCnt = UITabBarController()
        tabBarCnt.tabBar.barStyle = .default
//        let mapVC = MapViewController()
        let homeVC = HomeView()
//        mapVC.tabBarItem.title  = "Map"
        homeVC.title = "Home"
        tabBarCnt.viewControllers = [homeVC]
        
        self.view.addSubview(tabBarCnt.view)
        
    }
    func setUpBarStyle(){
//        would like to add corner radius
        
        
        let layer = CAShapeLayer()
//
//        layer.path = UIBezierPath(roundedRect: CGRect(x: 30, y: self.tabBarCnt.bounds.minY + 5, width: self.tabBar.bounds.width - 60, height: self.tabBar.bounds.height + 10), cornerRadius: (self.tabBar.frame.width/2)).cgPath
//        layer.shadowColor = UIColor.lightGray.cgColor
//        layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
//        layer.shadowRadius = 25.0
//        layer.shadowOpacity = 0.3
//        layer.borderWidth = 1.0
//        layer.opacity = 1.0
//        layer.isHidden = false
//        layer.masksToBounds = false
//        layer.fillColor = UIColor.white.cgColor
//
//        self.tabBarCnt.layer.insertSublayer(layer, at: 0)
//        if let items = self.tabBarCnt.items {
//          items.forEach { item in item.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -15, right: 0) }
//        }
//
//        self.tabBarCnt.itemWidth = 30.0
//        self.tabBarCnt.itemPositioning = .centered
    }
}

【问题讨论】:

  • 您可以发布您想要实现的示例设计吗?您似乎想为UITabBar 添加一些圆角半径。你能确认一下吗?
  • 您好,我想要一个类似此链接中提供的标签栏。 medium.com/@gemix95/…

标签: ios swift uikit uitabbarcontroller tabbar


【解决方案1】:

您可以修改UITabBarController中现有的tabBar。

func setUpBarStyle(){
    // Clipping needs to be enabled, otherwise rounded corners won't show.
    tabBarCnt.tabBar.clipsToBounds = true
    tabBarCnt.tabBar.layer.cornerRadius = 20
    // I guess you want to mask to top left and right corners. If not, change the maskedCorners to what you want.
    tabBarCnt.tabBar.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    相关资源
    最近更新 更多