【发布时间】:2017-11-29 15:22:39
【问题描述】:
我正在使用 UISearchBar 使用 Objective C 进行搜索。我需要以编程方式将搜索栏的占位符文本对齐到搜索栏的中心,并且当用户开始输入时,文本必须向左对齐。
提前致谢
【问题讨论】:
标签: ios objective-c uisearchbar
我正在使用 UISearchBar 使用 Objective C 进行搜索。我需要以编程方式将搜索栏的占位符文本对齐到搜索栏的中心,并且当用户开始输入时,文本必须向左对齐。
提前致谢
【问题讨论】:
标签: ios objective-c uisearchbar
我以前也遇到过和你一样的问题。我找不到任何解决方法,毕竟我只是使用了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
【讨论】: