how the Uisearchbar placeholder (placeholder) is displayed on the left.
when the project encountered this problem, although it is not a big knowledge point, but I think there should be friends tangled this problem, so here is the results of their own to share.
Starting with iOS7, the iOS system Uisearchbar components display placeholder icon hints and Magnifier are centered, and there is no corresponding method, properties for placeholder operation. This article provides a uisearchbar extension that allows placeholder content to reside on the left
Uisearchbar+extension.h
#import <UIKit/UIKit.h>
@interface uisearchbar (Extension)
-(void) Setleftplaceholder: (NSString *) placeholder;
@end
uisearchbar+extension.m
@implementation Uisearchbar (Jcsearchbarplaceholder)
-(void) Changeleftplaceholder: (NSString *) placeholder {
self.placeholder = placeholder;
SEL centerselector = nsselectorfromstring ([nsstring stringwithformat:@ "%@%@", @ "SetCenter", @ "placeholder:"]);
if ([self respondstoselector:centerselector]) {
BOOL centeredplaceholder = NO;
Nsmethodsignature *signature = [[Uisearchbar class] instancemethodsignatureforselector:centerselector];
Nsinvocation *invocation = [nsinvocation invocationwithmethodsignature:signature];
[Invocation settarget:self];
[Invocation setselector:centerselector];
[Invocation Setargument:¢eredplaceholder atindex:2];
[Invocation invoke];
}
@end
Finally, the #uisearchbar+jcsearchbarplaceholder.h header file is imported into the Uisearchbar class in the project.
You can then call the method to:
-(void) Changeleftplaceholder: (NSString *) placeholder;