how can I remove the top border on UIToolBar

后端 未结 11 699
一生所求
一生所求 2021-02-02 05:14

I have set my UIToolBar tint color to some value, and there is this border line that I see in which I want to remove:

相关标签:
11条回答
  • 2021-02-02 05:44

    The clipsToBounds technique clips the UIToolBar's shadow as well as the background view. On an iPhone X, that means the background no longer reaches outside the safe area.

    The solution below uses a mask to clip only the top of the UITabBar. The mask is rendered in a UIToolBar subclass, and the mask frame is kept updated in an override of layoutSubviews.

    class Toolbar: UIToolbar {
    
        fileprivate let maskLayer: CALayer = {
            let layer = CALayer()
            layer.backgroundColor = UIColor.black.cgColor
            return layer
        }()
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            initialize()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            initialize()
        }
    
        fileprivate func initialize() {
            layer.mask = maskLayer
            // Customize toolbar here
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            // height is an arbitrary number larger than the distance from the top of the UIToolbar to the bottom of the screen
            maskLayer.frame = CGRect(x: -10, y: 0, width: frame.width + 20, height: 500)
        }
    
    }
    

    0 讨论(0)
  • 2021-02-02 05:45

    Correct answer is the one by totalitarian...FYI. https://stackoverflow.com/a/14448645/627299

    My response is still below for reference.


    Here's what I did with my WHITE background toolbar...

    whiteToolBar.layer.borderWidth = 1;
    whiteToolBar.layer.borderColor = [[UIColor whiteColor] CGColor];    
    

    Perhaps you could do the same thing with your color instead.

    0 讨论(0)
  • 2021-02-02 05:47

    I got a bit confused with these answers but I was missing the point that you were using an Outlet so just to be clear here is the swift code I used to hide the border:

    import UIKit
    
    class ViewController: UIViewController {
    
        //MARK Outlets
        @IBOutlet weak var ToolBar: UIToolbar!
    
        //MARK View Functions
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            // Hide the bottom toolbar's top border
            ToolBar.clipsToBounds = true
        }
    }
    

    I had dragged a toolbar to the bottom of a view for this and it's not the top nav bar some other questions refer to.

    0 讨论(0)
  • 2021-02-02 05:48

    setShadowImage to [UIImage new]

    0 讨论(0)
  • 2021-02-02 05:52

    create a 1 pixel x 1 pixel clear image and call it clearPixel.png

    toolbar.setShadowImage(UIImage(named: "clearPixel.png"), forToolbarPosition: UIBarPosition.any)
    
    0 讨论(0)
  • 2021-02-02 05:52

    this doesn't work consistently on iOS versions, doesn't seem to work on iOS7. i answered this in another question: https://stackoverflow.com/a/19893602/452082 and you can modify that solution to just remove the background shadow (and leave your toolbar.backgroundColor whatever color you like)

    0 讨论(0)
提交回复
热议问题