简体   繁体   English

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

[英]How to add a 1 pixel gray border around a UISearchBar TextField

I'm trying to figure out how to add a 1 pixel stroke gray border to my UISearchBar in my app.我想弄清楚如何在我的应用程序中向我的 UISearchBar 添加 1 像素描边灰色边框。 The Facebook Messenger app accomplishes this quite well. Facebook Messenger 应用程序很好地实现了这一点。 (see pic below). (见下图)。

I know there is a background image property of UISearchBar, but I believe that is not the right thing to use, as it stretches the image out and repeats it across the entire view of the search bar.我知道 UISearchBar 有一个背景图像属性,但我认为这不是正确的使用方法,因为它会拉伸图像并在搜索栏的整个视图中重复它。

I'd greatly appreciate pointers in the right direction.我非常感谢正确方向的指示。

在此处输入图片说明

Try this code:试试这个代码:

//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;
        }
    }
}

Swift 3 version斯威夫特 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
        }
    }
}

To make it round you can use textField.layer.cornerRadius property为了使它圆你可以使用textField.layer.cornerRadius属性

This work fine:这项工作很好:

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

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

try this尝试这个

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

import Quartz Core Framework Reference into your project and add import <QuartzCore/QuartzCore.h> to your ViewController.m to work borderColor and borderWidth.将 Quartz Core Framework Reference 导入到您的项目中,并将 import <QuartzCore/QuartzCore.h>到您的 ViewController.m 以使用 borderColor 和 borderWidth。

Swift 5斯威夫特 5

Easy way to do简单的方法

    @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
            }
        }
    }

在此处输入图片说明

I upvoted Yaroslav's answer but it's broken as of iOS 13. Here is what works today :我赞成Yaroslav 的回答,但从 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"
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM