美文网首页
iOS 冷知识

iOS 冷知识

作者: 宁小缺 | 来源:发表于2017-07-05 15:35 被阅读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:@"企业名称"];
    

    相关文章

      网友评论

          本文标题:iOS 冷知识

          本文链接:https://www.haomeiwen.com/subject/nhsqhxtx.html