我希望UITabBar的顶部边框宽度为5.0,颜色为黄色。不要有左/底/右边框。
Tab Bar边框应该是平的(没有阴影或其他杂物)。
如何移除阴影(图像)线?
我希望UITabBar的顶部边框宽度为5.0,颜色为黄色。不要有左/底/右边框。
Tab Bar边框应该是平的(没有阴影或其他杂物)。
如何移除阴影(图像)线?
您可以按照以下方式在 FirstViewController.swift 中隐藏顶部边框:
self.tabBarController!.tabBar.layer.borderWidth = 0.50
self.tabBarController!.tabBar.layer.borderColor = UIColor.clear.cgColor
self.tabBarController?.tabBar.clipsToBounds = true
结果将会是:
之前:
之后:
希望对您有所帮助。
编辑:
您可以使用以下方式设置背景图片:
UITabBar.appearance().backgroundImage = UIImage(named: "yourImageWithTopYellowBorder.png")
UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
这就是我完成它的方式。我在UITabBar的顶部添加了一个子视图。
let lineView = UIView(frame: CGRect(x: 0, y: 0, width:tabBarController.tabBar.frame.size.width, height: 1))
lineView.backgroundColor = UIColor.yellow
tabBarController.tabBar.addSubview(lineView)
// The tabBar top border is done using the `shadowImage` and `backgroundImage` properties.
// We need to override those properties to set the custom top border.
// Setting the `backgroundImage` to an empty image to remove the default border.
tabBar.backgroundImage = UIImage()
// The `shadowImage` property is the one that we will use to set the custom top border.
// We will create the `UIImage` of 1x5 points size filled with the red color and assign it to the `shadowImage` property.
// This image then will get repeated and create the red top border of 5 points width.
// A helper function that creates an image of the given size filled with the given color.
// https://dev59.com/fF8d5IYBdhLWcg3wsT4b#39604716
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage
{
let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: size.width, height: size.height))
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
// Setting the `shadowImage` property to the `UIImage` 1x5 red.
tabBar.shadowImage = getImageWithColor(color: UIColor.red, size: CGSize(width: 1.0, height: 5.0))
SWIFT 3
我需要边框颜色(以及线条颜色和粗细)与我的应用程序中的其他元素匹配,因此在自定义UITabBarController的viewDidLoad中,以下代码适用于我:
tabBar.layer.borderWidth = 0.3
tabBar.layer.borderColor = UIColor(red:0.0/255.0, green:0.0/255.0, blue:0.0/255.0, alpha:0.2).cgColor
tabBar.clipsToBounds = true
UIView *borderLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 5.0)];
borderLine.backgroundColor = [UIColor yellowColor];
[self.tabBarController.tabBar addSubview:borderLine];
UITabBar
添加边框的方法。
它运作良好。Swift 4.2
self.tabBarController!.tabBar.layer.borderWidth = 0.50
self.tabBarController!.tabBar.layer.borderColor = UIColor.clear.cgColor
self.tabBarController?.tabBar.clipsToBounds = true
您可以根据需要更改边框颜色。
这是选项卡栏的阴影图像(属性)。尝试以下解决方案并查看。
** Swift **
//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil
// or
// Assing UIImage instance without image reference
UITabBar.appearance().shadowImage = UIImage()
** Objective-C **
//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];
// or
// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];
这是苹果关于shadowImage的指南。
@available(iOS 6.0, *)
open var shadowImage: UIImage?
iOS 6引入了一个名为shadowImage
的属性,您可以更改此属性以更改顶部边框。例如,您可以使用一个单色的1x1像素图像将顶部边框更改为该颜色:
UITabBar.appearance().shadowImage = UIImage(named: "TabBarShadow")
你还可以将其设置为 UIImage()
,以完全删除顶部边框。
UITabBar.appearance().shadowImage = UIImage()
extension UIImage {
func createSelectionIndicator(color: UIColor, size: CGSize, lineWidth: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: lineWidth))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
let tabBar = self.tabBarController!.tabBar
tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: UIColor.blue, size: CGSize(width: tabBar.frame.width/CGFloat(tabBar.items!.count), height: tabBar.frame.height) , lineWidth: 5.0)