如何在UISearchBar TextField周围添加1像素灰色边框

时间:2014-01-17 17:11:58

标签: ios objective-c uiview ios7 uisearchbar

我正在尝试弄清楚如何在我的应用中为我的UISearchBar添加1像素笔触灰色边框。 Facebook Messenger应用程序完成了这一点。 (见下图)。

我知道UISearchBar有一个背景图片属性,但我认为这不是正确的选择,因为它会拉伸图像并在搜索栏的整个视图中重复它。

我非常感谢指向正确的方向。

enter image description here

6 个答案:

答案 0 :(得分:12)

试试这段代码:

//First add the following import and macro:
#import <QuartzCore/QuartzCore.h>
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

//Then change yourSearchBar border: 
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    for (id object in [[[yourSearchBar subviews] objectAtIndex:0] subviews])
    {
        if ([object isKindOfClass:[UITextField class]])
        {
            UITextField *textFieldObject = (UITextField *)object;
            textFieldObject.layer.borderColor = [[UIColor grayColor] CGColor];
            textFieldObject.layer.borderWidth = 1.0;
            break;
        }
    }
}
else
{
    for (id object in [yourSearchBar subviews])
    {
        if ([object isKindOfClass:[UITextField class]])
        {
            UITextField *textFieldObject = (UITextField *)object;
            textFieldObject.layer.borderColor = [[UIColor grayColor] CGColor];
            textFieldObject.layer.borderWidth = 1.0;
            break;
        }
    }
}

答案 1 :(得分:8)

Swift 3 版本

override func viewDidLoad() {
    super.viewDidLoad()

    for s in searchBar.subviews[0].subviews {
        if s is UITextField {
            s.layer.borderWidth = 1.0
            s.layer.borderColor = UIColor.gray.cgColor
        }
    }
}

要实现它,您可以使用 textField.layer.cornerRadius 属性

答案 2 :(得分:3)

这项工作很好:

UITextField *txtSearchField = [yourSearchBar valueForKey:@"_searchField"];
[txtSearchField setBorderStyle:UITextBorderStyleRoundedRect];
txtSearchField.layer.cornerRadius = 4;
txtSearchField.layer.borderWidth = 1.0f;
txtSearchField.layer.borderColor = [[UIColor colorWhatYouWant] CGColor]; 

不要忘记#import <QuartzCore/QuartzCore.h>

答案 3 :(得分:2)

试试这个

self.poundsTxt.layer.borderColor = [[UIColor whiteColor]CGColor];
self.poundsTxt.layer.borderWidth=2.0;

将Quartz Core Framework Reference导入您的项目并添加 导入<QuartzCore/QuartzCore.h> 到你的ViewController.m工作borderColor和borderWidth。

答案 4 :(得分:1)

我赞成Yaroslav's answer,但在iOS 13上它已被打破。这是今天今天可用的功能:

    func changeSearchBarAppearance() {
        var searchTextField : UITextField?
        if #available(iOS 13, *) {
            // brand new searchTextField is available as of iOS 13
            searchTextField = searchBar.searchTextField
        } else {
            // Yaroslav's answer still works in iOS versions less than 13
            // here's a slightly different implementation:
            if let matchingTextField = searchBar.subviews[0].subviews.compactMap ({ $0 as? UITextField }).first {
                searchTextField = matchingTextField
            }
        }

        guard let validTextField = searchTextField else {
            return
        }

        validTextField.layer.cornerRadius = 16
        validTextField.layer.borderWidth = 1.0
        validTextField.layer.borderColor = UIColor.grey.cgColor
        validTextField.backgroundColor = .white
        validTextField.placeholder = "insert-some-placeholder-text"
    }

答案 5 :(得分:0)

快捷键5

简便的方法

    @IBOutlet private weak var searchBar:UISearchBar!{
        didSet{
            if let searchTextfield = self.searchBar.value(forKey: "searchField") as? UITextField  {
                searchTextfield.layer.borderColor = UIColor.lightGray.cgColor
                searchTextfield.layer.borderWidth = 1
                searchTextfield.layer.cornerRadius = 10
            }
        }
    }

enter image description here