iOS UISearchBar文本字段背景

时间:2012-12-12 18:44:37

标签: objective-c ios uikit uisearchbar

我正在尝试在iOS上自定义UISearchBar的文本字段,但我遇到了问题。使用以下方法:

[searchBar setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];

给我文本字段中的图像,但它是TINY。图像尺寸仅为40x15像素,用于拉伸。我正在创建如下图像:

UIImage *searchFieldImage = [[UIImage imageNamed:@"input_15x40.png"]
                  resizableImageWithCapInsets:UIEdgeInsetsMake(7, 19, 7, 20)];

然而,似乎这种外观修改不会拉伸图像 - 只需按原样使用。如何使用可伸缩图像自定义UISearchBar的文本域图像?

修改 我应该说杆是水平拉伸的,但它不是垂直拉伸的,因此只有15px的高度。

2 个答案:

答案 0 :(得分:4)

我在代码中看到的唯一问题是你创建名为searchFieldImage的var并将其用作searchField。

此代码对我有用:

UIImage *searchFieldImage = [[UIImage imageNamed:@"input_15x40.png"]
              resizableImageWithCapInsets:UIEdgeInsetsMake(7, 19, 7, 20)];

[searchBar setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];

-

编辑:

在高度上,图像不会拉伸,因为图像的高度将设置包裹它的视图的高度。

答案 1 :(得分:0)

let searchTextField = searchBar.valueForKey("_searchField") as? UITextField
searchTextField?.backgroundColor = UIColor.redColor()