2016-04-13 25 views
0

我想保留UISearchBar的占位符和搜索图标在所有版本的iOS中左对齐。那么,我该如何处理呢?如何保持UISearchBar的占位符与搜索图标左对齐

我试图将空白字符追加到UISearchBar的placeHolder,它似乎解决了这个问题,但我想知道是否存在优雅的方法来处理这个问题。

+0

嗨,诱骗UISerachbar方法是解决不了问题 –

+0

您可以自定义这样http://stackoverflow.com/questions/36542549/customize-search-bar/36543336#36543336搜索栏 –

回答

1

使用UItextField并根据需要自定义文本字段,而不是用空格来欺骗UISerachbar。

0
txtSearchBar = [self putsearchIconOnLeft: txtSearchBar]; 



-(UITextField*)putsearchIconOnLeft:(UITextField*)setTextField 
{ 
    UITextField *txt = setTextField; 
    UIView *viewTmp = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 10, 10)]; 

    UIImageView *imgSearch = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)]; 
    imgSearch.image = [UIImage imageNamed:@"icon-search.png"]; 
    [viewTmp addSubview:imgSearch]; 

    txt.leftView = viewTmp; 
    txt.leftViewMode = UITextFieldViewModeAlways; 
    return txt; 
}