我正在研究IOS 7应用程序。默认情况下它看起来像Pic(1)。但我需要将其更改为Pic(2)。我用Google搜索并找到了很少的答案,但它没有改变。或者否则我需要隐藏。所以我可以用背景图像管理。这是第一张图片
我使用下面的代码来修改它。但是没有成功。
在.h文件中
@property(nonatomic,strong) IBOutlet UISearchBar *findSearchBar;
在.m文件中
@synthesize findSearchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
[self setSearchIconToFavicon];
}
- (void)setSearchIconToFavicon
{
// The text within a UISearchView is a UITextField that is a subview of that UISearchView.
UITextField *searchField;
for (UIView *subview in self.findSearchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField)
{
UIView *searchIcon = searchField.leftView;
if ([searchIcon isKindOfClass:[UIImageView class]])
{
NSLog(@"aye");
}
searchField.rightView = nil;
searchField.leftView = nil;
searchField.leftViewMode = UITextFieldViewModeNever;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
}
我没有得到如何使视图图像的中心为零。它真的让我的时间过得很快。请帮助我。我出错了。
答案 0 :(得分:12)
UITextField *txfSearchField = [looksearchbar valueForKey:@"_searchField"];
[txfSearchField setBackgroundColor:[UIColor whiteColor]];
[txfSearchField setLeftViewMode:UITextFieldViewModeNever];
[txfSearchField setRightViewMode:UITextFieldViewModeNever];
[txfSearchField setBackground:[UIImage imageNamed:@"searchbar_bgImg.png"]];
[txfSearchField setBorderStyle:UITextBorderStyleNone];
//txfSearchField.layer.borderWidth = 8.0f;
//txfSearchField.layer.cornerRadius = 10.0f;
txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
txfSearchField.clearButtonMode=UITextFieldViewModeNever;
试试这可能会对你有帮助........
答案 1 :(得分:9)
我不确定如何左对齐占位符,但从iOS 5.0开始,这是一种简单,受支持的方式来修改搜索栏的文本字段属性,例如:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
将隐藏放大镜图标。
答案 2 :(得分:0)
你可以尝试:
searchBar.setImage(UIImage(named: "yourimage")!, forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)