ios 如何删除 UIToolBar 上的顶部边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9438864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I remove the top border on UIToolBar
提问by adit
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:
我已将 UIToolBar 色调颜色设置为某个值,并且我看到了要删除的边界线:


How do I remove this black border>
如何去除这个黑色边框>
采纳答案by Keenan
Correct answer is the one by totalitarian...FYI. https://stackoverflow.com/a/14448645/627299
正确答案是极权主义者的答案……仅供参考。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.
也许你可以用你的颜色做同样的事情。
回答by totalitarian
You can do like this:
你可以这样做:
self.navigationController.toolbar.clipsToBounds = YES;
回答by Ravi Mudaliar - Seffcon
toolbar1.clipsToBounds = YES;
Worked for me incase someone is still trying with Navigational bar
为我工作,以防有人仍在尝试使用导航栏
回答by Cherpak Evgeny
setShadowImage to [UIImage new]
将ShadowImage 设置为[UIImage new]
回答by DrMickeyLauer
Setting the style to UIBarStyleBlackTranslucent did it for me (iOS 6)
将样式设置为 UIBarStyleBlackTranslucent 为我做到了(iOS 6)
回答by damo
回答by alionthego
create a 1 pixel x 1 pixel clear image and call it clearPixel.png
创建一个 1 像素 x 1 像素的清晰图像并将其命名为 clearPixel.png
toolbar.setShadowImage(UIImage(named: "clearPixel.png"), forToolbarPosition: UIBarPosition.any)
回答by natbro
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/452082and you can modify that solution to just remove the background shadow (and leave your toolbar.backgroundColorwhatever color you like)
这在 iOS 版本上不一致,在 iOS7 上似乎也不起作用。我在另一个问题中回答了这个:https: //stackoverflow.com/a/19893602/452082,您可以修改该解决方案以删除背景阴影(并留下您toolbar.backgroundColor喜欢的任何颜色)
回答by Alex Givens
The clipsToBoundstechnique 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.
该clipsToBounds技术剪辑 UIToolBar 的阴影以及背景视图。在 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.
下面的解决方案使用遮罩仅剪辑 UITabBar 的顶部。遮罩在 UIToolBar 子类中呈现,遮罩框架在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)
}
}
回答by GazB
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:
我对这些答案有点困惑,但我忽略了您使用的是 Outlet 的这一点,所以这里要清楚的是,我用来隐藏边框的 swift 代码:
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.
为此,我已将工具栏拖到视图底部,但它不是其他一些问题所指的顶部导航栏。

