2015-09-01 23 views
9

我想UITabBar有一個寬度爲5.0的頂部邊框。邊框應該是黃色。 我不想要任何左/右/右邊框。如何更改我的UITabBar的頂部邊框?

標籤欄邊框應該是平的(沒有陰影或類似的東西)。

如何刪除陰影(圖像)線?

+0

創建您自己的TabBar子類,並在其上創建一個覆蓋頂部邊框。 –

回答

24

你能做到這樣,在您的FirstViewController.swift:

self.tabBarController!.tabBar.layer.borderWidth = 0.50 
self.tabBarController!.tabBar.layer.borderColor = UIColor.clearColor().CGColor 
self.tabBarController?.tabBar.clipsToBounds = true 

而結果將是:

前:

enter image description here

後:

enter image description here

希望它有幫助。

編輯:

您可以設置背景圖片是這樣的:

UITabBar.appearance().backgroundImage = UIImage(named: "yourImageWithTopYellowBorder.png") 
+0

這隱藏了頂部邊框,但我想要帶有顏色的邊框。 – TIMEX

+0

我認爲你可以設置背景圖像,其中有黃色上邊框。 –

+0

謝謝。圖像應該是什麼尺寸? – TIMEX

0

有一個名爲shadowImage這是在IOS 6中引入可以改變這個改變上邊框的屬性。例如,你可以使用一個1x1px圖像用單一顏色的上邊框更改爲顏色:

UITabBar.appearance().shadowImage = UIImage(named: "TabBarShadow") 

您還可以將其設置爲只UIImage()完全去除頂部邊框。

UITabBar.appearance().shadowImage = UIImage() 

要回答你的問題的5px邊框,這可以通過使用1x5px圖像。圖像大小似乎沒有限制,只會重複一次(例如,通過使用4x5px圖像(其中第一個2x5px是黑色,下一個2x5px是透明的)),您可以獲得虛線。請注意,如果你使用它,它超出了UITabBar的邊界,所以內容將會在圖像後面,除非你改變視圖邊界。

4

如果你想徹底刪除標籤欄,把這個在您的AppDelegate:

UITabBar.appearance().shadowImage = UIImage() 
UITabBar.appearance().backgroundImage = UIImage() 
3

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 
+3

不是正確的答案,這增加了底部,左側,右側和頂部的邊界 – Oscar

3

這是完整的解決方案,編制了不同的SO swers,這爲我工作(斯威夫特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)) 
2

只需設定UITabBar將backgroundImage和的ShadowImage是明確的顏色:

tabBar.shadowImage = UIImage.init(color: UIColor.clear) 
tabBar.backgroundImage = UIImage.init(color: UIColor.clear) 
0

它的TabBar的陰影圖像(屬性)。嘗試以下解決方案並參閱。

** **雨燕

//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? 

默認值是零。非零時,自定義陰影圖像顯示,而不是 默認陰影圖像。對於要示出的自定義陰影,定製 背景圖像還必須與-setBackgroundImage設置:(如果使用默認 背景圖像,默認的陰影的圖像將是 使用)。

1
UIView *borderLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 5.0)]; 
    borderLine.backgroundColor = [UIColor yellowColor]; 
    [self.tabBarController.tabBar addSubview:borderLine]; 

這是添加邊框,我跟隨UITabBar的方式。 它很酷。