在iOS 7 UISearchBar上,占位符文本居中,我想禁用它,并使其始终像以前一样粘在左边.
有一个私有的方法setCenterPlaceholder,在diff和调用这个与BOOL将做出旅程. https://gist.github.com/nscoding/7220368
- @interface NSCodingSearchBar : UISearchBar
- // Default by the system is YES.
- // https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UISearchBar.h
- @property (nonatomic,assign,setter = setHasCentredPlaceholder:) BOOL hasCentredPlaceholder;
- @end
执行文件
- #import "NSCodingSearchBar.h"
- // ------------------------------------------------------------------------------------------
- @implementation NSCodingSearchBar
- // ------------------------------------------------------------------------------------------
- #pragma mark - Initializers
- // ------------------------------------------------------------------------------------------
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if ((self = [super initWithFrame:frame]))
- {
- self.hasCentredPlaceholder = YES;
- }
- return self;
- }
- // ------------------------------------------------------------------------------------------
- #pragma mark - Methods
- // ------------------------------------------------------------------------------------------
- - (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder
- {
- _hasCentredPlaceholder = hasCentredPlaceholder;
- SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@",@"setCenter",@"Placeholder:"]);
- if ([self respondsToSelector:centerSelector])
- {
- NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
- [invocation setTarget:self];
- [invocation setSelector:centerSelector];
- [invocation setArgument:&_hasCentredPlaceholder atIndex:2];
- [invocation invoke];
- }
- }
- @end
我想知道是否有其他的,不是“哈奇的方式”做同样的事情.