美文网首页
ios中UISearchBar的placeHolder左对齐

ios中UISearchBar的placeHolder左对齐

作者: 与瑾 | 来源:发表于2017-09-18 18:42 被阅读156次

    实现方法

    重写UISearchBar,添加一个是否居中显示PlaceHolder的属性,然后利用NSMethodSignature这个类修改对齐的属性

    @interfaceNSCodingSearchBar:UISearchBar/// 是否居中显示@property(nonatomic,assign, setter = setHasCentredPlaceholder:)BOOLhasCentredPlaceholder;@end

    #pragma mark - Initializers// ------------------------------------------------------------------------------------------- (instancetype)initWithFrame:(CGRect)frame

    {if((self= [superinitWithFrame:frame]))

    {self.hasCentredPlaceholder=NO;

    }returnself;

    }// ------------------------------------------------------------------------------------------#pragma mark - Methods// ------------------------------------------------------------------------------------------- (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder

    {

    _hasCentredPlaceholder = hasCentredPlaceholder;

    SEL centerSelector = NSSelectorFromString([NSStringstringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);if([selfrespondsToSelector:centerSelector])

    {

    NSMethodSignature *signature = [[UISearchBarclass] instanceMethodSignatureForSelector:centerSelector];

    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setTarget:self];

    [invocation setSelector:centerSelector];

    [invocation setArgument:&_hasCentredPlaceholder atIndex:2];

    [invocation invoke];

    }

    }@end

    相关文章

      网友评论

          本文标题:ios中UISearchBar的placeHolder左对齐

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