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

时间:2014-03-27 05:18:46

标签: ios objective-c uisearchbar

我想为UISearchBar(灰色部分)的背景视图应用角半径,如下图所示。我知道将角半径应用于文本字段,但如何应用背景灰色部分?是否可以这样做?

enter image description here

1 个答案:

答案 0 :(得分:-1)

您可以像上面那样制作图像(searchbar_Img.png)并设置为搜​​索栏,如下所示。(或者)您可以将圆角矩形设置为文本字段

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

希望它可以帮助你..