我使用UISearchBar
进行搜索,使用目标C.我需要搜索栏的占位符文本以编程方式与搜索栏的中心对齐,当用户开始输入时,文本必须左对齐。< / p>
先谢谢
答案 0 :(得分:1)
我之前遇到了和你一样的问题。我无法找到任何解决方法,毕竟我只使用了UILabel
和UISearchBarDelegate
方法。
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UISearchBarDelegate>
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController () {
UISearchBar *srchBar;
UILabel *customPlaceHolderForSearchBar;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frm = CGRectMake(0, 0, self.view.frame.size.width, 80);
srchBar = [[UISearchBar alloc] initWithFrame:frm];
srchBar.delegate = self;
[self.view addSubview:srchBar];
customPlaceHolderForSearchBar = [[UILabel alloc] initWithFrame:frm];
customPlaceHolderForSearchBar.text = @"PlaceHolder text";
customPlaceHolderForSearchBar.textColor = [UIColor grayColor];
customPlaceHolderForSearchBar.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:customPlaceHolderForSearchBar];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
customPlaceHolderForSearchBar.hidden = YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
if (searchBar.text.length < 1) {
customPlaceHolderForSearchBar.hidden = NO;
}
}
@end