删除uisearchbar中uitextfield的边框 [英] remove the border of uitextfield in uisearchbar

查看:187
本文介绍了删除uisearchbar中uitextfield的边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义UISearchBar控件到一个样式,如下图所示:

I wanna custom the UISearchBar control to a style as showed in the picture below:

首先,我应该删除后台视图,然后在UISearchBar中找到UITextfiled,扩展UITextfiled的框架,删除它的边界。

First I should remove the background view, then find out the UITextfiled in UISearchBar, extend the frame of UITextfiled and remove the boarder of it.

这是我的代码:

UITextField *searchField;

NSUInteger numViews = [self.subviews count];
for(int i = 0; i != numViews; i++) {
    if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
        searchField = [self.subviews objectAtIndex:i];
    }
}
if(!(searchField == nil)) {

    [searchField setBorderStyle:UITextBorderStyleNone];
    searchField.frame = self.frame; //modify the frame
}

[[self.subviews objectAtIndex:0]removeFromSuperview]; //remove the background.

结果是UITextField的边框仍然存在。 :(

The result is the boarder of the UITextField is still there. :(

>

我的问题是:
1.为什么我不能修改UISearchBar的UITextField的外观?

My question is : 1.Why can not I modify the appearance of the UITextField of UISearchBar?

2.如何在UISearchBar中删除UITextfield的边界。

2.How to remove the boarder of UITextfield in UISearchBar.

非常感谢!

推荐答案

子类UISearchBar并覆盖layoutSubViews以删除搜索栏背景和文本字段边框子视图:

Subclass UISearchBar and override layoutSubViews to remove the search bar background and the text field border subviews:

- (void)layoutSubviews
{    
    [super layoutSubviews];

    for (UIView *subview in self.subviews) {
        // Remove the default background
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
        }

        // Remove the rounded corners
        if ([subview isKindOfClass:NSClassFromString(@"UITextField")]) {
            UITextField *textField = (UITextField *)subview;
            for (UIView *subsubview in textField.subviews) {
               if ([subsubview isKindOfClass:NSClassFromString(@"UITextFieldBorderView")]) {
                    [subsubview removeFromSuperview];
                }
            }
        }
    }
}

这篇关于删除uisearchbar中uitextfield的边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆