2012-01-25 132 views
2

我是iphone新手,我想更改搜索栏背景以清除颜色,如何将其更改为清除颜色。我的代码是:如何更改ipad中的搜索栏背景颜色

UISearchBar *mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(10, 550, 330, 42)]; 
mySearchBar.delegate = self; 
mySearchBar.showsCancelButton = YES; 
[email protected]"Search here.."; 
    mySearchBar.tintColor=[UIColor clearColor]; 
[self.view addSubview:mySearchBar]; 

在此先感谢:

回答

4

因为搜索栏中有一个默认的视图,我们删除该视图,则背景颜色是在IOS 7明确

for (UIView *subview in mySearchBar.subviews) { 
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
        [subview removeFromSuperview]; 
        break; 
    } 
}   
+0

感谢您回复我.. ..它的工作很好.. – user1162034

+0

Thanx投票上来 – Deepesh

0

可以使用着色颜色设置,尝试设置色调颜色,每回合的背景颜色。

mySearchBar.tintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0]; 
+0

非常感谢你@vishy ..它的工作正常。谢谢很多.. – user1162034

0
mySearchBar.backgroundColor = [UIColor clearColor]; 

而不是设置tintColor的设置搜索栏的backgroundColor。

0

我做了这样的事情。

if (self.searchDisplayController.searchBar.subviews.count >= 1) { 
     for (UIView *subview in [[self.searchDisplayController.searchBar.subviews objectAtIndex:0] subviews]) { 

      if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
       [subview removeFromSuperview]; 
       break; 
      } 
     } 
    }