实现方法
重写UISearchBar,添加一个是否居中显示PlaceHolder的属性,然后利用NSMethodSignature这个类修改对齐的属性
@interfaceNSCodingSearchBar:UISearchBar/// 是否居中显示@property(nonatomic,assign, setter = setHasCentredPlaceholder:)BOOLhasCentredPlaceholder;@end
#pragma mark - Initializers// ------------------------------------------------------------------------------------------- (instancetype)initWithFrame:(CGRect)frame
{if((self= [superinitWithFrame:frame]))
{self.hasCentredPlaceholder=NO;
}returnself;
}// ------------------------------------------------------------------------------------------#pragma mark - Methods// ------------------------------------------------------------------------------------------- (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder
{
_hasCentredPlaceholder = hasCentredPlaceholder;
SEL centerSelector = NSSelectorFromString([NSStringstringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);if([selfrespondsToSelector:centerSelector])
{
NSMethodSignature *signature = [[UISearchBarclass] instanceMethodSignatureForSelector:centerSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:centerSelector];
[invocation setArgument:&_hasCentredPlaceholder atIndex:2];
[invocation invoke];
}
}@end
网友评论