nsc*_*ing 14 cocoa-touch uisearchbar ios
在iOS 7 UISearchBar上,占位符文本居中,我想禁用它并使它始终像以前一样粘在左边.

在diffs上有一个私有方法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
Run Code Online (Sandbox Code Playgroud)
实施文件
#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
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何其他的,而不是"Hacky方式"做同样的事情.
我还没有尝试过,UISearchBar但这适用于 a UITextField。我对它进行了子类化并覆盖了leftViewRectForBounds:, textRectForBounds:and editingRectForBounds:,这是代码:
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect inset = CGRectMake(bounds.origin.x + kLeftTextPadding,
bounds.origin.y,
bounds.size.width - kLeftTextPadding,
bounds.size.height);
return inset;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
10373 次 |
| 最近记录: |