swift 如何在UITabbar中设置左上角和右上角的圆角半径?

92dk7w1h  于 2023-01-16  发布在  Swift
关注(0)|答案(5)|浏览(382)

我花了几个小时才弄明白。然而,它没有发生,最后,我不得不来到这里。需要实现两件事
首先,我想有一个自发的圆角半径在顶部(这基本上是右上角和左上角)的UITabbar
其次,我想有一个阴影以上的圆角半径(如下图所示)。
请看下图

如果我这边还有什么需要,请告诉我,我一定会提供的。
任何帮助都将不胜感激。

    • 编辑1**

还有一个小问题出现在这里,假设,即使,然而,我们能够做到这一点,苹果审查小组会接受应用程序吗?我有点紧张和好奇.

wbgh16ku

wbgh16ku1#

问:还有一个小问题出现在沿着,假设,即使,然而,我们能够做到这一点,苹果审查团队会接受应用程序吗?答:是的,他们接受你的应用程序,我有添加这种标签栏.
创建自定义选项卡栏
主页选项卡控制器

import UIKit

class HomeTabController: UITabBarController
{
    var viewCustomeTab : CustomeTabView!
    var lastSender : UIButton!

    //MARK:- ViewController Methods
    override func viewDidLoad()
    {
        super.viewDidLoad()

        UITabBar.appearance().shadowImage = UIImage()

        allocateTabItems()
    }

    //MARK:- Prepare Methods
    // Allocate shop controller with tab bar
    func allocateTabItems()
    {

        let vc1 = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "Avc") as? Avc
        let item1 = UINavigationController(rootViewController: vc1!)
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.viewControllers = [item1]
        createTabBar()
    }

    func createTabBar()
    {
        viewCustomeTab = CustomeTabView.instanceFromNib()
        viewCustomeTab.translatesAutoresizingMaskIntoConstraints = false
        viewCustomeTab.call()

        self.view.addSubview(viewCustomeTab)

        if #available(iOS 11, *)
        {
            let guide = view.safeAreaLayoutGuide
            NSLayoutConstraint.activate([guide.bottomAnchor.constraint(equalToSystemSpacingBelow: viewCustomeTab.bottomAnchor, multiplier: 0), viewCustomeTab.leadingAnchor.constraint(equalToSystemSpacingAfter: guide.leadingAnchor, multiplier: 0), guide.trailingAnchor.constraint(equalToSystemSpacingAfter: viewCustomeTab.trailingAnchor, multiplier: 0), viewCustomeTab.heightAnchor.constraint(equalToConstant: 70) ])
        }
        else
        {
            let standardSpacing: CGFloat = 0
            NSLayoutConstraint.activate([viewCustomeTab.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: standardSpacing), bottomLayoutGuide.topAnchor.constraint(equalTo: viewCustomeTab.bottomAnchor, constant: standardSpacing)
                ])
        }

        viewCustomeTab.btnTab1.addTarget(self, action: #selector(HomeTabController.buttonTabClickAction(sender:)), for: .touchUpInside)
        viewCustomeTab.btnTab2.addTarget(self, action: #selector(HomeTabController.buttonTabClickAction(sender:)), for: .touchUpInside)
        viewCustomeTab.btnTab3.addTarget(self, action: #selector(HomeTabController.buttonTabClickAction(sender:)), for: .touchUpInside)
        viewCustomeTab.btnTab4.addTarget(self, action: #selector(HomeTabController.buttonTabClickAction(sender:)), for: .touchUpInside)
        viewCustomeTab.btnTab5.addTarget(self, action: #selector(HomeTabController.buttonTabClickAction(sender:)), for: .touchUpInside)

        //self.view.layoutIfNeeded()
        viewCustomeTab.layoutIfNeeded()
        viewCustomeTab.btnTab1.alignContentVerticallyByCenter(offset: 3)
        viewCustomeTab.btnTab2.alignContentVerticallyByCenter(offset: 3)
        viewCustomeTab.btnTab3.alignContentVerticallyByCenter(offset: 3)
        viewCustomeTab.btnTab4.alignContentVerticallyByCenter(offset: 3)
        viewCustomeTab.btnTab5.alignContentVerticallyByCenter(offset: 3)
        viewCustomeTab.btnTab1.isSelected = true
    }

    //MARK:- Button Click Actions
    //Manage Tab From Here
    func setSelect(sender:UIButton)
    {
        viewCustomeTab.btnTab1.isSelected = false
        viewCustomeTab.btnTab2.isSelected = false
        viewCustomeTab.btnTab3.isSelected = false
        viewCustomeTab.btnTab4.isSelected = false
        viewCustomeTab.btnTab5.isSelected = false
        sender.isSelected = true
    }

    @objc func buttonTabClickAction(sender:UIButton)
    {
        //self.selectedIndex = sender.tag
        if sender.tag == 0
        {
            let vc1 = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "Bvc") as? Bvc

             let item1 = UINavigationController(rootViewController: vc1!)
            item1.navigationBar.isHidden = false
            self.viewControllers = [item1]
            setSelect(sender: viewCustomeTab.btnTab1)
            return
        }

        if sender.tag == 1
        {
            let vc2 = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "Cvc") as? Cvc
            let item2 = UINavigationController(rootViewController: vc2!)
            item2.navigationBar.isHidden = false
            item2.navigationBar.isTranslucent = false
            self.viewControllers = [item2]
            setSelect(sender: viewCustomeTab.btnTab2)
            return
        }

        if sender.tag == 2
        {
            let vc3 = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "Dvc") as? Dvc
            let item3 = UINavigationController(rootViewController: vc3!)
            item3.navigationBar.isHidden = false
             item3.navigationBar.isTranslucent = false
            self.viewControllers = [item3]
            setSelect(sender: viewCustomeTab.btnTab3)
            return
        }

        if sender.tag == 3
        {

        }

        if sender.tag == 4
        {

        }
    }
}

Create Custom View For Shadow Effect and For + Button. 

import UIKit

class CustomeTabView: UIView
{
    @IBOutlet weak var btnTab5: UIButton!
    @IBOutlet weak var btnTab4: UIButton!
    @IBOutlet weak var btnTab3: UIButton!
    @IBOutlet weak var btnTab2: UIButton!
    @IBOutlet weak var btnTab1: UIButton!
    @IBOutlet weak var vRadius: UIView!

    class func instanceFromNib() -> CustomeTabView
    {
        return UINib(nibName: "CustomeTabView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomeTabView
    }

    private var shadowLayer: CAShapeLayer!

    override func layoutSubviews()
    {
        super.layoutSubviews()

        let shadowSize : CGFloat = 2.0

        let shadowPath = UIBezierPath(roundedRect: CGRect(x: -shadowSize / 2,   y: -shadowSize / 2, width: self.vRadius.frame.size.width, height: self.vRadius.frame.size.height), cornerRadius : 20)

        self.vRadius.layer.masksToBounds = false
        self.vRadius.layer.shadowColor = UIColor.black.cgColor
        self.vRadius.layer.shadowOffset = CGSize.zero//(width: self.vRadius.frame.size.width, height: self.vRadius.frame.size.height)
        self.vRadius.layer.shadowOpacity = 0.5
        self.vRadius.layer.shadowPath = shadowPath.cgPath
        self.vRadius.layer.cornerRadius = 20
      }

OpenImg

rur96b6h

rur96b6h2#

斯威夫特4.2
你可以用一些自定义视图和自定义标签栏控制器来实现这一点。你可以通过只编辑自定义视图来自定义颜色和阴影。

自定义选项卡栏控制器

import UIKit
class MainTabBarController: UITabBarController{

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        tabBar.backgroundImage = UIImage.from(color: .clear)
        tabBar.shadowImage = UIImage()

        let tabbarBackgroundView = RoundShadowView(frame: tabBar.frame)
        tabbarBackgroundView.cornerRadius = 25
        tabbarBackgroundView.backgroundColor = .white
        tabbarBackgroundView.frame = tabBar.frame
        view.addSubview(tabbarBackgroundView)

        let fillerView = UIView()
        fillerView.frame = tabBar.frame
        fillerView.roundCorners([.topLeft, .topRight], radius: 25)
        fillerView.backgroundColor = .white
        view.addSubview(fillerView)

        view.bringSubviewToFront(tabBar)
    }

圆形阴影视图

import UIKit

class RoundShadowView: UIView {

    let containerView = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        layoutView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func layoutView() {

        // set the shadow of the view's layer
        layer.backgroundColor = UIColor.clear.cgColor
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 0, height: -8.0)
        layer.shadowOpacity = 0.12
        layer.shadowRadius = 10.0
        containerView.layer.cornerRadius = cornerRadius
        containerView.layer.masksToBounds = true

        addSubview(containerView)
        containerView.translatesAutoresizingMaskIntoConstraints = false

        // pin the containerView to the edges to the view
        containerView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        containerView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        containerView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    }
}

UI图像扩展

import UIKit

extension UIImage {
    static func from(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img!
    }
}
xmakbtuz

xmakbtuz3#

要添加任何半径或形状,你可以使用UIBezierPath。我举的例子有带半径的左右角,如果你愿意,你可以使用更多可设计的个性化设置。

@IBDesignable class TabBarWithCorners: UITabBar {
    @IBInspectable var color: UIColor?
    @IBInspectable var radii: CGFLoat = 15.0

    private var shapeLayer: CALayer?

    override func draw(_ rect: CGRect) {
        addShape()
    }

    private func addShape() {
        let shapeLayer = CAShapeLayer()
    
        shapeLayer.path = createPath()
        shapeLayer.strokeColor = UIColor.gray.withAlphaComponent(0.1).cgColor
        shapeLayer.fillColor = color?.cgColor ?? UIColor.white.cgColor
        shapeLayer.lineWidth = 1
    
        if let oldShapeLayer = self.shapeLayer {
            layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
        } else {
            layer.insertSublayer(shapeLayer, at: 0)
        }
    
        self.shapeLayer = shapeLayer
    }

    private func createPath() -> CGPath {
        let path = UIBezierPath(
            roundedRect: bounds,
            byRoundingCorners: [.topLeft, .topRight],
            cornerRadii: CGSize(width: radii, height: 0.0))
    
        return path.cgPath
    }
}
tez616oj

tez616oj4#

Swift 5.3.1、XCode 11+、iOS 14用于情节提要:

import UIKit

class CustomTabBar: UITabBar {
        
     override func awakeFromNib() {
            super.awakeFromNib()
            layer.masksToBounds = true
            layer.cornerRadius = 20
            layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
      }
 }
y3bcpkx1

y3bcpkx15#

子类化UITabBarController,然后概述viewWillLayoutSubviews()并添加此代码。

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.tabBar.layer.masksToBounds = true
    self.tabBar.layer.cornerRadius = 12 // whatever you want
    self.tabBar.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner] // only the top right and left corners
}

这将是

的结果

相关问题