2012-10-30 106 views
9

我需要一個帶有取消按鈕的完全透明的搜索欄。我已經嘗試了很多解決方案但我還找不到更好的解決方案。當我嘗試刪除背景顏色時,它顯示範圍欄。可以任何一個給我一些源代碼完全transperant搜索欄與罐頭按鈕。 這裏的如何在UISearchbar中更改清除按鈕圖像(x按鈕)或至少清除按鈕圖像的顏色?

addSearchbar.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]; 
UITextField *sbTextField = (UITextField *)[self.addSearchbar.subviews lastObject]; 

for (UIView *subview in addSearchbar.subviews) 
{ 
    NSLog(@"%@",subview); 
    if ([subview isKindOfClass:[UITextField class]]) 
    { 
     sbTextField = (UITextField *)subview; 
     UIImage *image = [UIImage imageNamed: @"06_magnifying_glass.png"]; 
     UIImageView *iView = [[UIImageView alloc] initWithImage:image]; 
     iView.frame = CGRectMake(0, 0, 24, 24); 
     sbTextField.leftView.frame = CGRectMake(0, 0, 24, 24); 
     sbTextField.leftView = iView; 
     [sbTextField.rightView removeFromSuperview]; 
     CGFloat myWidth = 24.0f; 
     CGFloat myHeight = 24.0f; 
     UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myWidth, myHeight)]; 
     [myButton setImage:[UIImage imageNamed:@"clear.png"] forState:UIControlStateNormal]; 
     [myButton setImage:[UIImage imageNamed:@"clear.png"] forState:UIControlStateHighlighted]; 

     [myButton addTarget:self action:@selector(doClear:) forControlEvents:UIControlEventTouchUpInside]; 
     sbTextField.rightView = myButton; 
     sbTextField.rightViewMode = UITextFieldViewModeWhileEditing; 
     break; 
    } 
    if([subview isMemberOfClass:[UISegmentedControl class]]) 
    { 
     UISegmentedControl *scopeBar=(UISegmentedControl *) subview; 
     scopeBar.tintColor = [UIColor clearColor]; 
    } 
} 
[sbTextField removeFromSuperview]; 
[addSearchbar addSubview:sbTextField]; 
[addSearchbar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchBar.png"] forState:UIControlStateNormal]; 
CGRect sbFrame = self.addSearchbar.frame; 
// Set the default height of a textfield 
sbFrame.size.height = 31; 

/* 8 is the top padding for textfield inside searchbar 
* You may need to add a variable to 8 according to your requirement. 
*/ 
sbFrame.origin.y = 6+self.addSearchbar.frame.origin.y; 
sbTextField.frame = sbFrame; 
sbTextField.textColor = [UIColor lightGrayColor]; 
[sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin]; 

需要一個完全透明的搜索欄與取消按鈕和清除按鈕,但沒有scopebar。提前

+0

http://stackoverflow.com/questions/9704562/remove-or-覆蓋在uisearchdisplaycontroller-in-a-uipopove –

+0

我發現瞭解決方案,但另一個pblm,所以我更新了我的問題 – Arvind

回答

24

由於作爲的iOS 5.0,你可以做到以下幾點:

UIImage *imgClear = [UIImage imageNamed:@"clear"]; 
[addSearchBar setImage:imgClear forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal]; 

完蛋了。您可能還想重複UIControlStateHighlighted狀態的行。

你應該做的最後一件事是在搜索欄的子視圖內部挖掘。它保證有一天會打破。使用適當的API來定製任何控件。

+2

不幸的是,你的答案的後半部分不起作用。 [見這裏](http://stackoverflow.com/questions/14720026/customizing-uisearchbariconclear-for-uicontrolstatehighlighted-does-notwork)。 – Dima

+0

在iOS 7.1這個解決方案http://stackoverflow.com/questions/13862050/custom-clear-button-in-uisearchbar-text-field#answer-19438100完美。 – Alex

+0

您必須爲高亮和正常狀態使用不同的圖像。我的意思是,圖像可以是相同的,但不是相同的UIImage *參考。 – ingaham

4

看到您先設置了UIControlHighlighted狀態圖像,然後是UIControlStateNormal狀態圖像,否則您可能會遇到clearIcon未設置爲突出顯示狀態的問題。 (不知道爲什麼會出現這個問題)

[_searchBar setImage:[UIImage imageNamed:@"ClearIcon"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted]; 
[_searchBar setImage:[UIImage imageNamed:@"ClearIcon"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal]; 
2

對於那些你在斯威夫特嘗試這種(我知道有人會來這裏尋找這個...)在這裏你去:

self.searchBar.setImage(UIImage(named: "ico-cancel"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal) 
    self.searchBar.setImage(UIImage(named: "ico-cancel"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Highlighted) 

確保您的Assets.xcassets文件夾中有正確的圖像。

0

這裏是你如何改變放大鏡和清除按鈕圖標在一個UISearchBar的的UITextField 如果沒有新的圖標

// Reference search display controller search bar's text field (in my case). 
UITextField *searchBarTextField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"]; 

// Magnifying glass icon. 
UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView; 
leftImageView.image = [LeftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
leftImageView.tintColor = [UIColor whiteColor]; 

    // Clear button 
UIButton *clearButton = [searchBarTextField valueForKey:@"_clearButton"]; 
[clearButton setImage:[clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; 
clearButton.tintColor = [UIColor whiteColor];