आईओएस 7 पर UISearchBar प्लेसहोल्डर टेक्स्ट केंद्रित है और मैं इसे अक्षम करना चाहता हूं और इसे हमेशा बाईं ओर चिपकाना चाहता हूं जैसा कि पहले था।आईओएस 7 पर UISearchBar प्लेसहोल्डर पाठ संरेखित छोड़ दिया?
एक निजी विधि setCenterPlaceholder, नहीं है डिफ पर और एक 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
यदि आपका कोई और नहीं, "Hacky रास्ता" वही चीज़ है मैं सोच रहा हूँ।
यह बेहतर यदि आप विचाराधीन कोड शामिल है, के बजाय Searchbar के पाठ फ़ील्ड फ्रेम के आकार को पढ़ने के लिए असंभव लगता है बस इसे जोड़ने के लिए। –
क्या आपने इसे आजमाया? http://stackoverflow.com/a/19555305/2155985 –
मुझे यह अच्छा विचार नहीं लगता है। आपको जितना संभव हो मानकों के साथ प्रयास करना चाहिए। – anders