iOS 冷知识

2017-07-05  本文已影响26人  宁小缺

1.placeHolder靠左

建立一个类别,名字自取,并在.h文件中创建一个方法
@interface UISearchBar (MySearchBarPlaceholder)
-(void)changeLeftPlaceholder:(NSString *)placeholder;

.m文件中实现方法
-(void)changeLeftPlaceholder:(NSString *)placeholder {
    self.placeholder = placeholder;
    SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);
    if ([self respondsToSelector:centerSelector]) {
        BOOL centeredPlaceholder = NO;
        NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        [invocation setTarget:self];
        [invocation setSelector:centerSelector];
        [invocation setArgument:&centeredPlaceholder atIndex:2];
        [invocation invoke];
    }
}
在要使用的视图导入类别
    #import "UISearchBar+MySearchBarPlaceholder.m"
在要使用的位置直接调用
    [_searchBar changeLeftPlaceholder:@"企业名称"];
上一篇下一篇

猜你喜欢

热点阅读