如何iBooks喜欢在桌子上搜索UI

Ani*_*das 3 iphone search uisearchbar

我想在iBooks中显示tableview的搜索栏.如何减少搜索栏的宽度以及如何在没有任何背景颜色的情况下显示它.

另外,如何在显示页面时隐藏搜索框.

在此输入图像描述

Nic*_*ver 6

我可以想到两个选择:

通过继承UITextfield创建自己的

  • 将边框样式设置为UITextBorderStyleNone
  • 将leftView设置为放大镜图像
  • 将背景设置为透明的png,仅显示圆形边框
  • 使用如果uiimages stretchableImageWithLeftCapWidth:topCapHeight方法使得背景图像在拉伸时看起来很漂亮

与UISearchbar混在一起,不推荐

您可以通过以下方式访问UISearchbar的子视图:

for (UIView *aSubview in [searchbar subviews]) {
            // this will remove the toolbar around the searchbar in iOS 4
    if ([aSubview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    {
        [aSubview removeFromSuperview];
    }       

    // get a grip on the underlying textfield...
    if ([aSubview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) 
    {
        UITextField *aTextfield = (UITextField *)aSubview;

        // this won't do anything at it is already none, just experimenting
        aTextfield.borderStyle = UITextBorderStyleNone;

        // this will remove the whole border as it is a custom background
        aTextfield.background = nil;

        // this will remove the magnifying glass
        aTextfield.leftView = nil;

        // play around even more :)
    }       
}
Run Code Online (Sandbox Code Playgroud)

您可以隐藏搜索栏

  • 将hidden属性设置为YES
  • 将框架或中心属性设置为可见区域之外的某个位置
  • 将alpha属性设置为0.0
  • 仅在需要时将搜索栏添加到视图中