如何更改UITabBar的顶部边框?

TIM*_*MEX 17 uitabbarcontroller uitabbar ios swift

我希望UITabBar有一个宽度为5.0的顶部边框.边框应为黄色.我不想要任何左/下/右边框.

标签栏边框应该是平的(没有阴影或类似的东西).

如何删除阴影(图像)线?

Dha*_*esh 35

你可以在FirstViewController.swift中这样做:

self.tabBarController!.tabBar.layer.borderWidth = 0.50
self.tabBarController!.tabBar.layer.borderColor = UIColor.clearColor().CGColor
self.tabBarController?.tabBar.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)

结果将是:

之前:

在此输入图像描述

后:

在此输入图像描述

希望能帮助到你.

编辑:

您可以这样设置背景图像:

UITabBar.appearance().backgroundImage = UIImage(named: "yourImageWithTopYellowBorder.png")
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 9

如果你想完全删除标签栏,把它放在你的 AppDelegate 中:

UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
Run Code Online (Sandbox Code Playgroud)


Uda*_*Sri 6

这就是我完成它的方式。我在 UITabBar 顶部添加了一个子视图。

var lineView = UIView(frame: CGRect(x: 0, y: 0, width:tabBarController.tabBar.frame.size.width, height: 1))
lineView.backgroundColor = UIColor.yellow
tabBarController.tabBar.addSubview(lineView)
Run Code Online (Sandbox Code Playgroud)


iur*_*rii 5

这是完整的解决方案,汇集了不同的SO答案,对我有用(Swift 3):

// 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.
// http://stackoverflow.com/a/39604716/1300959
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))
Run Code Online (Sandbox Code Playgroud)