【问题标题】:Rectangular UISearchBar on iOS 7iOS 7 上的矩形 UISearchBar
【发布时间】:2014-03-08 21:03:30
【问题描述】:

我正在尝试制作一个 UISearchBar 矩形而不是圆形,但到目前为止我发现的所有解决方案(主要是遍历子视图)在 iOS 7 上似乎都被破坏了。

我自己做了一些研究,结果发现它只有一个UIView 子视图,它有额外的子视图、一个UISearchBarBackground 和一个UISearchBarTextField(它们都是私有类)。 我试过了

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

if ([view conformsToProtocol:@protocol(UITextInputTraits)]) {
    @try {

        [(UITextField *)view setBorderStyle:UITextBorderStyleRoundedRect];
    }
    @catch (NSException * e) {
        // ignore exception
    }
}

其中view 是那个UIView 子视图的子视图,但它们似乎都不起作用。

【问题讨论】:

  • 事实证明,部分问题是我将 UISearchBar 的搜索样式(在 IB 中)设置为最小。如果将其设置为 Prominent,则第一段代码有效。 (实际上,它仍然是四舍五入的,所以不是完美的解决方案。如果有人有更好的想法,请分享。)
  • 您可能希望将其发布为答案。
  • @Marco 查看我对该评论的编辑,这不是最好的解决方案,但如果暂时没有人提出更好的解决方案,我会发布它。
  • 你试过UISearchBar的外观方法吗,比如setSearchFieldBackgroundImage:forState:
  • @Rickye 我误会了,以为你已经解决了你的问题。

标签: ios objective-c uiview ios7 uisearchbar


【解决方案1】:

试试这个...(我知道它也在使用子视图,但它在 ios7 中工作)

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 49)];
[self.view addSubview:searchBar];
[self checkSubViewsOfView:searchBar WithTabIndex:@""];

并添加此方法

-(void)checkSubViewsOfView:(UIView *)view WithTabIndex:(NSString *)strTab
{
    if ([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")])
    {
        view.layer.borderWidth = 1;
        view.layer.borderColor = [[UIColor whiteColor] CGColor];
        return;
    }

    for (UIView *vvv in view.subviews)
    {
        NSLog(@"%@%@",strTab,[vvv description]);

        if (vvv.subviews > 0)
        {
            NSString *str = [NSString stringWithFormat:@"____%@",strTab];
            [self checkSubViewsOfView:vvv WithTabIndex:str];
        }
    }
}

【讨论】:

  • 好的,谢谢!很接近我的结果。这样做的问题是它取消了 UISearchBar 的大小调整,因此取消按钮永远不会出现在它旁边。
  • @Rickye 如果您使用这种方法。这是坏的。因为它不是来自公共文档。所以使用 Thorsten 说的方法是最好的
【解决方案2】:

您可以像这样设置搜索字段背景:

[self.searchBar setSearchFieldBackgroundImage:[[UIImage imageNamed:@"searchbar_stretch-0-10-0-10"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forState:UIControlStateNormal];

和这样的搜索栏背景:

[self.searchBar setBackgroundImage:[UIImage imageNamed:@"categories_navbar"]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-01
    • 2015-09-10
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    相关资源
    最近更新 更多