2017-11-29 16 views

답변

0

나는 이전과 같은 문제가있었습니다. 어떤 해결 방법을 찾을 수 없으며 결국에는 UILabelUISearchBarDelegate 개의 메소드를 사용했습니다.

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