欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS 开发之UISearchBar 详解及实例

程序员文章站 2023-12-20 23:45:34
ios uisearchbar 详解 iphone开发之uisearchbar学习是本文要学习的内容,主要介绍了uisearchbar的使用,不多说,我们先来看详细内容。...

ios uisearchbar 详解

iphone开发之uisearchbar学习是本文要学习的内容,主要介绍了uisearchbar的使用,不多说,我们先来看详细内容。关于uisearchbar的一些问题。

1、修改uisearchbar的背景颜色

uisearchbar是由两个subview组成的,一个是uisearchbarbackground,另一个是uitextfield. 要ib中没有直接操作背景的属性。方法是直接将 uisearchbarbackground移去 

seachbar=[[uisearchbar alloc] init]; 
seachbar.backgroundcolor=[uicolor clearcolor]; 
for (uiview *subview in seachbar.subviews)  
{  
if ([subview iskindofclass:nsclassfromstring(@"uisearchbarbackground")]) 
    {  
[subview removefromsuperview];  
break; 
}  
} 

第二种解决的方法:

[[searchbar.subviews objectatindex:0]removefromsuperview]; 

2、

uisearchbar* m_searchbar = [[uisearchbar alloc] initwithframe:cgrectmake(0, 44, 320, 41)]; 
m_searchbar.delegate = self; 
m_searchbar.barstyle = uibarstyleblacktranslucent; 
m_searchbar.autocorrectiontype = uitextautocorrectiontypeno; 
m_searchbar.autocapitalizationtype = uitextautocapitalizationtypenone; 
m_searchbar.placeholder = _(@"search"); 
m_searchbar.keyboardtype = uikeyboardtypedefault; 
//为uisearchbar添加背景图片 
uiview *segment = [m_searchbar.subviews objectatindex:0]; 
uiimageview *bgimage = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"images/search_bar_bg.png"]]; 
[segment addsubview: bgimage]; 
//<---背景图片 
[self.view addsubview:m_searchbar]; 
[m_searchbar release]; 

3:取消uisearchbar调用的键盘

[searchbar resignfirstresponder]; 

添加uisearchbar的两种方法:

代码

uisearchbar *mysearchbar = [[uisearchbar alloc] 
initwithframe:cgrectmake(0.0, 0.0, self.view.bounds.size.width, 45)];     
 mysearchbar.delegate = self;     
 mysearchbar.showscancelbutton = no;     
 mysearchbar.barstyle=uibarstyledefault;     
 mysearchbar.placeholder=@"enter name or categary";      
mysearchbar.keyboardtype=uikeyboardtypenamephonepad;      
[self.view addsubview:mysearchbar];     
 [mysearchbar release];  

在 tableview上添加:  

代码 

 //add table 
    uitableview *mybeaconstableview = [[uitableview alloc]  
                      initwithframe:cgrectmake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-40)  
                       style:uitableviewstyleplain]; 
    mybeaconstableview.backgroundcolor = [uicolor whitecolor]; 
    mybeaconstableview.delegate=self; 
    mybeaconstableview.datasource=self; 
    [mybeaconstableview setrowheight:40]; 
    // add searchbar  
    searchbar = [[uisearchbar alloc] initwithframe:cgrectmake(0.0, 0.0, self.view.bounds.size.width, 40)]; 
    searchbar.placeholder=@"enter name"; 
    searchbar.delegate = self; 
    mybeaconstableview.tableheaderview = searchbar; 
    searchbar.autocorrectiontype = uitextautocorrectiontypeno; 
    searchbar.autocapitalizationtype = uitextautocapitalizationtypenone; 
    [searchbar release]; 
    [self.view addsubview:mybeaconstableview]; 
    [mybeaconstableview release]; 

小结:iphone开发之uisearchbar学习的内容介绍完了,希望本文对你有所帮助

上一篇:

下一篇: