自定义UIDatePicker for iOS7的文本颜色(就像Mailbox一样)

rny*_*rom 36 uidatepicker uipickerview uikit ios uiappearance

我陷入了最令人沮丧的困境.我上下研究过,可以清楚地看到Apple不希望我们篡改iOS 7.好吧,我想篡改.而且,Mailbox的团队清楚地知道如何做到这一点并获得批准.

我想要实现的主要是将标签颜色更改为白色.

邮箱UIDatePicker

我的第一个想法是他们使用的是模仿UIDatePicker的自定义UIPickerView,但我不认为是这种情况.

我放大了一个小片段,发现了正常UIDatePicker(黑线)的残余以及字母"W"上的剪辑.

邮箱UIDatePicker片段

现在我已经高高低调了.做了一些运行时黑客攻击,与UIAppearance混淆,甚至挖到一些私有API只是为了看看这是否可行.

我接近,非常接近,但它使用了私有API,如果你滚动得足够快,标签会再次变黑.

如果没有a)违反规则或b)花费无数小时重新实现UIDatePicker,我完全不知道如何做到这一点.

邮箱,告诉我你的秘密!如果其他人有任何建议(我的意思是任何建议),请告诉我.

此外,这是我得到的最接近的:

到目前为止如此接近

Sig*_*Sig 59

我需要与我的应用程序类似,并且最终走了很长一段路.真的很遗憾没有一种简单的方法可以简单地切换到UIDatePicker的白色文本版本.

当setTextColor:消息发送到标签时,下面的代码使用UILabel上的类别强制标签的文本颜色为白色.为了不对应用程序中的每个标签执行此操作,我已将其过滤为仅适用于它是UIDatePicker类的子视图.最后,一些标签在添加到超级视图之前已经设置了颜色.要捕获这些代码,请覆盖willMoveToSuperview:方法.

将下面的内容分成多个类别可能会更好,但为了便于发布,我在这里添加了所有内容.

#import "UILabel+WhiteUIDatePickerLabels.h"
#import <objc/runtime.h>

@implementation UILabel (WhiteUIDatePickerLabels)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceSelector:@selector(setTextColor:)
                      withNewSelector:@selector(swizzledSetTextColor:)];
        [self swizzleInstanceSelector:@selector(willMoveToSuperview::)
                      withNewSelector:@selector(swizzledWillMoveToSuperview:)];
    });
}

// Forces the text colour of the lable to be white only for UIDatePicker and its components
-(void) swizzledSetTextColor:(UIColor *)textColor {
    if([self view:self hasSuperviewOfClass:[UIDatePicker class]] ||
       [self view:self hasSuperviewOfClass:NSClassFromString(@"UIDatePickerWeekMonthDayView")] ||
       [self view:self hasSuperviewOfClass:NSClassFromString(@"UIDatePickerContentView")]){
        [self swizzledSetTextColor:[UIColor whiteColor]];
    } else {
        //Carry on with the default
        [self swizzledSetTextColor:textColor];
    }
}

// Some of the UILabels haven't been added to a superview yet so listen for when they do.
- (void) swizzledWillMoveToSuperview:(UIView *)newSuperview {
    [self swizzledSetTextColor:self.textColor];
    [self swizzledWillMoveToSuperview:newSuperview];
}

// -- helpers --
- (BOOL) view:(UIView *) view hasSuperviewOfClass:(Class) class {
    if(view.superview){
        if ([view.superview isKindOfClass:class]){
            return true;
        }
        return [self view:view.superview hasSuperviewOfClass:class];
    }
    return false;
}

+ (void) swizzleInstanceSelector:(SEL)originalSelector
                 withNewSelector:(SEL)newSelector
{
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method newMethod = class_getInstanceMethod(self, newSelector);

    BOOL methodAdded = class_addMethod([self class],
                                       originalSelector,
                                       method_getImplementation(newMethod),
                                       method_getTypeEncoding(newMethod));

    if (methodAdded) {
        class_replaceMethod([self class],
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 有人在Appstore上发布过这段代码吗? (6认同)
  • 天才谢谢!Apple是否有机会批准这一点? (3认同)
  • 该应用程序自2014年12月起获得苹果批准:) (2认同)

art*_*gor 30

这是一个很好的工作片段.测试上的iOS 7.1,8.08.1.

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_8_1
    #error "Check if this hack works on this OS"
#endif

    [self.datePicker setValue:[UIColor whiteColor] forKeyPath:@"textColor"];

    SEL selector = NSSelectorFromString(@"setHighlightsToday:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
    BOOL no = NO;
    [invocation setSelector:selector];
    [invocation setArgument:&no atIndex:2];
    [invocation invokeWithTarget:self.datePicker];
Run Code Online (Sandbox Code Playgroud)

我已经添加了一个简单的条件来打破编译过程,如果你正在为iOS> 8.1构建,因为我不能确定这个hack是否会起作用,并且你不希望因为这个而在生产中出现任何崩溃.

使用setHighlightsToday:选择器是因为默认情况下UIDatePicker使用选择器[UIColor blackColor]来显示当前日期.


Kle*_*men 14

我找到了将字体颜色设置为任何颜色的技巧,也没有搞乱今日标签.

datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
Run Code Online (Sandbox Code Playgroud)

结果如下:

此解决方案的所有学分都转到其他帖子的这篇文章:https: //stackoverflow.com/a/33459744/1236334


fnc*_*c12 10

self.birthDatePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
Run Code Online (Sandbox Code Playgroud)

这对我有用.