61

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:

enter image description here

How do I remove this black border>

adit
  • 32,574
  • 72
  • 229
  • 373

11 Answers11

145

You can do like this:

self.navigationController.toolbar.clipsToBounds = YES;
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
totalitarian
  • 3,606
  • 6
  • 32
  • 55
  • 2
    That didn't help me in iOS 6 – DrMickeyLauer Apr 19 '13 at 10:24
  • 2
    This works for a bare UIToolbar as well; doesn't have to be part of a navigation controller. – J. Perkins Nov 04 '13 at 15:29
  • Works like a charm on iOS 7.x with SDK 7. – eonil Jun 25 '14 at 08:50
  • *Nico*, this is working because "clipsToBounds" makes a view draw only within it's bounds. in this case, a layer's border draws outside of the view's bounds, so settings "clipsToBounds" to true "disable" it's border. – Ariel Jan 02 '17 at 10:10
  • 4
    `clipsToBounds = YES` breaks a toolbar's look on the iPhone X. It hides the top border, but it also stops the toolbar's background from extending to the bottom edge of the screen. – ksm Nov 09 '17 at 13:16
  • In my case, I am just using the toolbar as the `inputAccessoryView` for a text field (displayed above the keyboard), so the bottom safe area is no issue. – Nicolas Miari Oct 31 '18 at 04:49
50
toolbar1.clipsToBounds = YES;  

Worked for me incase someone is still trying with Navigational bar

slfan
  • 8,950
  • 115
  • 65
  • 78
21

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.

Keenan
  • 376
  • 4
  • 15
17

setShadowImage to [UIImage new]

Cherpak Evgeny
  • 2,659
  • 22
  • 29
  • 4
    Without a doubt, the proper answer – beebcon Dec 23 '16 at 17:29
  • 2
    This is the one you need to use to avoid losing toolbar color in safe areas such as at bottom of iPhone X i.e. [self setShadowImage:UIImage.new forToolbarPosition:UIBarPositionAny]; – gpdawson Oct 17 '18 at 04:33
12
navigationController?.toolbar.barTintColor = .white

navigationController?.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)

enter image description here

damo
  • 849
  • 12
  • 16
4

Setting the style to UIBarStyleBlackTranslucent did it for me (iOS 6)

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67
3

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

toolbar.setShadowImage(UIImage(named: "clearPixel.png"), forToolbarPosition: UIBarPosition.any)
alionthego
  • 8,508
  • 9
  • 52
  • 125
2

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)

Community
  • 1
  • 1
natbro
  • 1,038
  • 12
  • 13
2

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.

Incorrect UIToolBar on iPhone X

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

}

Correct UIToolBar on iPhone X

Alex
  • 527
  • 4
  • 10
  • This works for me on any device BUT the iPhone X. I copied the code line-for-line, creating a new Swift class and specifying the class in Interface Builder. – E-Madd Nov 06 '17 at 20:10
  • @E-Madd Perhaps the Interface Builder is the difference. In our setup, we are programmatically creating the UINavigationController with a UINavigationBar and UIToolbar subclass, like so: `let navigationController = UINavigationController(navigationBarClass: NavigationBar.self, toolbarClass: Toolbar.self)` – Alex Nov 07 '17 at 23:16
  • I refactored to this with no change. I still have the border. Actually, it seems to be a gap between the main view and the toolbar. – E-Madd Nov 09 '17 at 16:53
1

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.

GazB
  • 3,510
  • 1
  • 36
  • 42
0
[[UIToolbar appearance] setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];

[[UIToolbar appearance] setShadowImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionBottom];

[UIToolbar appearance].barTintColor = [UIColor ...];```
coolcool1994
  • 3,704
  • 4
  • 39
  • 43