iOS解决runtime导致设置按钮的状态颜色不正确的问题

如下代码,按钮颜色应该为默认的绿色,但是运行显示为红色。最后有验证了下,这三行代码谁在最后面写,按钮的颜色就为什么颜色。研究了一下午,不知道是什么原因。最后才发现项目里面导入了一个UIButton+TitleColor文件。因为UIButton+TitleColor文件里面写了+ (void)load {}方法,所以UIButton+TitleColor即使不导入,也会执行+ (void)load {}方法,这是苹果的机制。正是因为执行了+ (void)load {}方法里面的内容,所以才导致,按钮的状态颜色,谁在最后,就是什么颜色。特简写了如下三种情况

情况1:按钮显示为红色

[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];// 1
[titleButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];// 2
[titleButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];// 3

情况2:按钮显示为黑色

[titleButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];// 1
[titleButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];// 2
[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];// 3

情况3:按钮显示为绿色

[titleButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];// 2
[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];// 3
[titleButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];// 1

PS:以上三种情况,都显示绿色才正确。就是说应该显示默认状态下的文字颜色。


出现问题的详情代码(实际上代码是没有问题的,只是导入了UIButton+TitleColor文件的原因)



//
// ViewController.m
//  Hema
//
//  Created by zhangbin on 2018/7/25.
//  Copyright © 2018年 MoShi_N. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

/** 标题栏 */
@property (nonatomic, strong) UIView *titlesView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 标题栏
    [self setupTitlesView];
    
}


/**
 *  标题栏
 */
- (void)setupTitlesView
{
    self.titlesView = [[UIView alloc] init];
    self.titlesView.frame = CGRectMake(0, 64, self.view.frame.size.width, 50);
    self.titlesView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.titlesView];
    
    // 标题按钮
    [self setupTitleButtons];
    
}


/**
 *  标题按钮。  创建只会执行一次
 */
- (void)setupTitleButtons{
    
    NSArray *titles = @[@"推荐职位", @"全部职位",@"ff大幅度",@"3232",@"将大幅度发"];
    NSUInteger count = titles.count;
    CGFloat titleButtonH = self.titlesView.frame.size.height;
    CGFloat titleButtonW = self.titlesView.frame.size.width / count;
    
    for (NSInteger i = 0; i < count; i++) {
        
        UIButton *titleButton = [[UIButton alloc]init];
        
        // 高亮状态下的按钮文字颜色
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
      [titleButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
      
        titleButton.tag = i;
        titleButton.selected = NO;
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        titleButton.frame = CGRectMake(i * titleButtonW, 0, titleButtonW, titleButtonH);
        [self.titlesView addSubview:titleButton];
    }
}

@end


iOS解决runtime导致设置按钮的状态颜色不正确的问题_第1张图片
image.png

出现以上问题,完全是因为之前导入了如下文件。将该文件从项目里面删除,就可以解决按钮的状态颜色不正确的问题。

UIButton+TitleColor.h文件

//
//  UIButton+TitleColor.h
//  UIButton+TitleColor
//
//  Copyright (c) 2015 Draveness. All rights reserved.
//
//  These files are generated by ruby script, if you want to modify code
//  in this file, you are supposed to update the ruby code, run it and 
//  test it. And finally open a pull request.

#import 

@interface UIButton (TitleColor)

/**
 * Set this property when switch to night version uibutton TitleColor turns to this color.
 */
@property (nonatomic, strong) UIColor *nightTitleColor;

/**
 *  UIButton TitleColor in normal version.
 */
@property (nonatomic, strong, readonly) UIColor *normalTitleColor;

@end

UIButton+TitleColor.m文件

//
//  UIButton+TitleColor.m
//  UIButton+TitleColor
//
//  Copyright (c) 2015 Draveness. All rights reserved.
//
//  These files are generated by ruby script, if you want to modify code
//  in this file, you are supposed to update the ruby code, run it and
//  test it. And finally open a pull request.

#import "UIButton+TitleColor.h"
#import "DKNightVersionManager.h"
#import "objc/runtime.h"

@interface UIButton ()

@property (nonatomic, strong) UIColor *normalTitleColor;

@end

@implementation UIButton (TitleColor)

+ (void)load {
    static dispatch_once_t onceToken;                                              
    dispatch_once(&onceToken, ^{                                                   
        Class class = [self class];                                                
        SEL originalSelector = @selector(setTitleColor:forState:);                                  
        SEL swizzledSelector = @selector(hook_setTitleColor:forState:);                                 
        Method originalMethod = class_getInstanceMethod(class, originalSelector);  
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);  
        BOOL didAddMethod =                                                        
        class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));                   
        if (didAddMethod){
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));           
        } else {                                                                   
            method_exchangeImplementations(originalMethod, swizzledMethod);        
        }
    });
    [DKNightVersionManager addClassToSet:self.class];
}

- (void)hook_setTitleColor:(UIColor*)titleColor forState:(UIControlState)state {
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNormal) [self setNormalTitleColor:titleColor];
    [self hook_setTitleColor:titleColor forState:UIControlStateNormal];
}

- (void)saveNormalColor {
    self.normalTitleColor = self.currentTitleColor;
}

- (UIColor *)nightTitleColor {
    UIColor *nightColor = objc_getAssociatedObject(self, @selector(nightTitleColor));
    if (nightColor) {
        return nightColor;
    } else {
        UIColor *resultColor = self.normalTitleColor ?: [UIColor whiteColor];
        return resultColor;
    }
}

- (void)setNightTitleColor:(UIColor *)nightTitleColor {
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) [self setTitleColor:nightTitleColor forState:UIControlStateNormal];
    objc_setAssociatedObject(self, @selector(nightTitleColor), nightTitleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)normalTitleColor {
    return objc_getAssociatedObject(self, @selector(normalTitleColor));
}

- (void)setNormalTitleColor:(UIColor *)normalTitleColor {
    objc_setAssociatedObject(self, @selector(normalTitleColor), normalTitleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

你可能感兴趣的:(iOS解决runtime导致设置按钮的状态颜色不正确的问题)