如何在UISearchBar TextField周围添加1像素灰色边框 [英] How to add a 1 pixel gray border around a UISearchBar TextField

查看:96
本文介绍了如何在UISearchBar TextField周围添加1像素灰色边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to figure out how to add a 1 pixel stroke gray border to my UISearchBar in my app. The Facebook Messenger app accomplishes this quite well. (see pic below).

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

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.

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

I'd greatly appreciate pointers in the right direction.

推荐答案

试试这段代码:

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

这篇关于如何在UISearchBar TextField周围添加1像素灰色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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