首页 > 代码库 > Hacky way 解决 iOS 7 UISearchBar 默认文本居左展示问题
Hacky way 解决 iOS 7 UISearchBar 默认文本居左展示问题
参考:ON IOS 7 UISEARCHBAR LEFT ALIGN PLACEHOLDER TEXT?
控件代码:https://gist.github.com/CoCrash/de56f5f0b70b7eb6d6e3
一个私有方法setCenterPlaceholder,在调用时修改这个布尔值可以实现修改位置的效果。
1 @interface NSCodingSearchBar : UISearchBar2 3 // Default by the system is YES.4 // https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UISearchBar.h5 @property (nonatomic, assign, setter = setHasCentredPlaceholder:) BOOL hasCentredPlaceholder;6 7 @end
实现文件:
1 @implementation NSCodingSearchBar 2 3 4 // ------------------------------------------------------------------------------------------ 5 #pragma mark - Initializers 6 // ------------------------------------------------------------------------------------------ 7 - (instancetype)initWithFrame:(CGRect)frame 8 { 9 if ((self = [super initWithFrame:frame]))10 {11 self.hasCentredPlaceholder = YES;12 }13 14 return self;15 }16 17 18 // ------------------------------------------------------------------------------------------19 #pragma mark - Methods20 // ------------------------------------------------------------------------------------------21 - (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder22 {23 _hasCentredPlaceholder = hasCentredPlaceholder;24 25 SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);26 if ([self respondsToSelector:centerSelector])27 {28 NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];29 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];30 [invocation setTarget:self];31 [invocation setSelector:centerSelector];32 [invocation setArgument:&_hasCentredPlaceholder atIndex:2];33 [invocation invoke];34 }35 36 }37 38 39 @end
Hacky way 解决 iOS 7 UISearchBar 默认文本居左展示问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。