原技术贴: https://cathra.github.io/2016/03/17/UISearchBar-Placeholder-Left-align/
直接看效果:
原效果:

居左后:

写一个UIsearchbar 的扩展类,

.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];
}
}
网友评论