2013-03-12 37 views
4

很長一段時間以來,我一直試圖無效地刪除我的UISearchBar上的有害線。它出現在一個黑暗的背景光背景或光線暗的:線以上UISearchBar

enter image description hereenter image description here

我看着this question,但它沒有任何關係與分離器或拉來刷新。當我最終決定放棄並尋求幫助時,我又開始探索,發現了一個非常簡單(儘管可能不是完全直觀)的解決方案,所以我想我會分享它,以防其他人面臨同樣的問題。我配置的搜索欄,如下所示:

// I tried this drawing method 
[self.searchBar setBackgroundImage:[[UIImage imageNamed:@"linen_bg_bar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]]; 
// and this one. 
[self.searchBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"linen_bg_bar.png"]]]; 
// Same problem either way. 

[self.searchBar setPlaceholder:NSLocalizedString(@"Filter", @"placeholder text in the search bar")]; 
[self.searchBar setSearchFieldBackgroundImage:[[UIImage imageNamed:@"linen_textfield_search.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(16, 16, 16, 16)] forState:UIControlStateNormal]; 
[self.searchBar setImage:[UIImage imageNamed:@"linen_icon_search.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 
self.table.tableHeaderView = self.searchBar; 
[self.table setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"linen_bg_dark.png"]]]; 

this post's的建議,我想在它上面繪製,但它似乎繪圖仍然得到了呈現該線下方。

CGRect rect = self.searchBar.frame; 
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 2)]; 
lineView.backgroundColor = [UIColor redColor]; 

見我的紅色的「障眼法」行的例子:

enter image description here

回答

10

我不肯定這會工作,但它是有道理的基礎上,不必設置爲掩蓋視圖 y值。

searchBar.clipsToBounds = YES; 
+1

完美的作品!但對我來說,這似乎很奇怪,搜索欄在界限之外劃出一條線。 – enjayem 2013-03-13 16:17:22

+0

@enjayem我知道。你是否特意將'clipsToBounds'變成了某個地方? – Undo 2013-03-13 19:18:37

+0

不是。我所做的只是設置自定義背景圖像。 – enjayem 2013-03-13 20:38:02

1

事實證明,我所要做的就是調整掩蓋線的框架開始1pt 以上搜索欄,我在搜索欄頂部有一條漂亮的1pt灰線。

CGRect rect = self.searchBar.frame; 
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, -1, rect.size.width, 1)]; 
lineView.backgroundColor = [UIColor colorWithHexString:@"353535" alpha:1.0]; 
[self.searchBar addSubview:lineView]; 

瞧。

+0

仍然看起來不對...... – Undo 2013-03-12 23:19:58

+0

我絕對可以接受其他解決方案。這是我能夠想到的最好的。 – enjayem 2013-03-13 01:41:58

+0

我不是說我可以做得更好。我只是說這個解決方案看起來有點亂。 – Undo 2013-03-13 03:35:15

2

希望這有助於:

self.searchBar.layer.borderColor = Color.CGColor; 
self.searchBar.layer.borderWidth = 1; 

,你可以在你的searchController

1

在我來說,我是在代碼中創建一個UISearchBar並將其放置在導航欄的右邊找到搜索欄屬性酒吧按鈕項目。將背景圖像設置爲空白圖像將移除1像素行。

searchBar.backgroundImage = UIImage()