ios UIsearchbar如何使放大镜图片和placeholder文字默认居左

原技术贴: https://cathra.github.io/2016/03/17/UISearchBar-Placeholder-Left-align/

直接看效果:
原效果:


ios UIsearchbar如何使放大镜图片和placeholder文字默认居左_第1张图片
369E0D1A-671C-40F2-8CC2-53D0F949E1C1.png

居左后:

ios UIsearchbar如何使放大镜图片和placeholder文字默认居左_第2张图片
E9C3146C-0C95-4554-984A-A9AEF70C0A9C.png

写一个UIsearchbar 的扩展类,


DA7D9BBA-30CB-45CF-8E5B-DCE4AD8E43AF.png

.h文件

@interface UISearchBar (Extension)
-(void)setLeftPlaceholder:(NSString *)placeholder;
@end

.m文件实现

-(void)setLeftPlaceholder:(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];
}

}

你可能感兴趣的:(ios UIsearchbar如何使放大镜图片和placeholder文字默认居左)