Skip to content

Instantly share code, notes, and snippets.

@nscoding
Last active July 6, 2018 04:07
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nscoding/7220368 to your computer and use it in GitHub Desktop.
Save nscoding/7220368 to your computer and use it in GitHub Desktop.
On iOS 7 UISearchBar the placeholder text is centred and I want to disable that and make it always stick to the left like it was before. There is a private method setCenterPlaceholder and calling this with a BOOL work will make the trick. I am wondering if there is any other, not "Hacked" that does the same thing.
@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
@EdSancha
Copy link

EdSancha commented Aug 5, 2015

Have you shipped it in any app? I wonder if it can be rejected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment