简体   繁体   English

如何在iOS中为UISearchBar背景视图应用拐角半径?

[英]How to apply corner radius for UISearchBar background view in iOS?

I want to apply a corner radius for the background view of the UISearchBar(the gray part) like the image below.I know applying corner radius to the text field, but how to apply for the background gray part? 我想为UISearchBar(灰色部分)的背景视图应用拐角半径,如下图所示。我知道将拐角半径应用于文本字段,但是如何申请背景灰色部分呢? Is it possible to do so? 有可能这样做吗?

在此处输入图片说明

You can make image like above(searchbar_Img.png) and set to search bar as below.. (OR) you can make rounded rect to your text field 您可以像上面那样制作图像(searchbar_Img.png)并按如下所示设置为搜索栏。(OR)您可以对文本字段进行四舍五入

     UITextField *txfSearchField = [yoursearchbarref valueForKey:@"_searchField"];
    [txfSearchField setBackgroundColor:[UIColor whiteColor]];
    [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
    [txfSearchField setRightViewMode:UITextFieldViewModeNever];
    [txfSearchField setBackground:[UIImage imageNamed:@"searchbar_Img.png"]];

    [txfSearchField setBorderStyle:UITextBorderStyleNone];
    //txfSearchField.layer.borderWidth = 8.0f;
    //txfSearchField.layer.cornerRadius = 10.0f;
    txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
    txfSearchField.clearButtonMode=UITextFieldViewModeNever;

(OR) (要么)

for (UIView *searchBarSubview in [searchbar subviews])
    {
        if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)])
        {
            @try
            {
                [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];

            }
            @catch (NSException * e)
            {
                // ignore exception
            }
        }
    }

Hope it helps you.. 希望对您有帮助。

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

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