How to set top left and right corner radius with desired drop shadow in UITabbar?

后端 未结 5 698
萌比男神i
萌比男神i 2021-01-05 03:05

I\'ve spent almost a couple of hours to figure it out. However, it did not happen and finally, I had to come here. Two things are required to be achieved: <

5条回答
  •  既然无缘
    2021-01-05 03:31

    Swift 4.2

    You can achieve this with some custom view with a custom tab bar controller. You can customize the colors and shadows by editing only the custom views.

    Custom Tab Bar Controller

    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)
        }
    

    Rounded Shadow View

    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
        }
    }
    

    UIImage extension

    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!
        }
    }
    
    

提交回复
热议问题