0
목표 C를 사용하여 검색 목적으로 UISearchBar
을 사용하고 있습니다. 검색 막대의 자리 표시 자 텍스트가 프로그래밍 방식으로 검색 막대 중심에 정렬되어야하며 사용자가 입력을 시작하면 텍스트가 있어야합니다. 왼쪽 정렬.UISearchBar 자리 표시 자 텍스트 - 프로그램 중심으로 정렬
미리 감사드립니다.
목표 C를 사용하여 검색 목적으로 UISearchBar
을 사용하고 있습니다. 검색 막대의 자리 표시 자 텍스트가 프로그래밍 방식으로 검색 막대 중심에 정렬되어야하며 사용자가 입력을 시작하면 텍스트가 있어야합니다. 왼쪽 정렬.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