reference: On IOS 7 Uisearchbar left ALIGN PLACEHOLDER TEXT?
Control code: HTTPS://GIST.GITHUB.COM/COCRASH/DE56F5F0B70B7EB6D6E3
A private method, Setcenterplaceholder, modifies this boolean value at the time of invocation to achieve the effect of modifying the position.
1 @interface Nscodingsearchbar:uisearchbar 2 3 // Default by the system is YES. 4 // Https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UISearchBar.h 5 @property (nonatomic, assign, setter = Sethascentredplaceholder:) BOOL Hascentredplaceholder; 6 7 @end
Implementation file:
1 @implementationNscodingsearchbar2 3 4 // ------------------------------------------------------------------------------------------5 #pragmaMark-initializers6 // ------------------------------------------------------------------------------------------7-(Instancetype) initWithFrame: (CGRect) Frame8 {9 if(self =[Super Initwithframe:frame]))Ten { OneSelf.hascentredplaceholder =YES; A } - - returnSelf ; the } - - - // ------------------------------------------------------------------------------------------ + #pragmaMark-methods - // ------------------------------------------------------------------------------------------ +- (void) Sethascentredplaceholder: (BOOL) Hascentredplaceholder A { at_hascentredplaceholder =Hascentredplaceholder; - -SEL centerselector = nsselectorfromstring ([NSString stringWithFormat:@"%@%@",@"SetCenter",@"Placeholder:"]); - if([self respondstoselector:centerselector]) - { -Nsmethodsignature *signature = [[Uisearchbarclass] instancemethodsignatureforselector:centerselector]; inNsinvocation *invocation =[Nsinvocation invocationwithmethodsignature:signature]; - [invocation settarget:self]; to [invocation setselector:centerselector]; +[Invocation Setargument:&_hascentredplaceholder Atindex:2]; - [invocation invoke]; the } * $ }Panax Notoginseng - the @end
Hacky to solve IOS 7 Uisearchbar default text left display problem