alibaba/Tangram-iOS

layout不支持background表达式形式

lllhhh opened this issue · 1 comments

layout不支持background表达式形式,例:

background="@{${tagBgColor} ? ${tagBgColor} : #ffffff }"

问题已解决,是因为VVLayout.m文件中未实现接收表达式传值的方法,既:

- (BOOL)setStringData:(NSString *)data forKey:(int)key;
  • 解决方法1:在VVLayout.m中实现此方法:
- (BOOL)setStringData:(NSString *)data forKey:(int)key{
   BOOL ret = NO;
   if (key == STR_ID_background) {
       self.backgroundColor = [UIColor vv_colorWithString:data] ?: [UIColor blackColor];
       ret = YES;
   }
   return ret;
}
  • 解决方法2:新建VVLayout分类,利用runtime进行方法交换,达到动态修复此bug:
#import "VVLayout+Tracking.h"
#import <VirtualView/VVLayout.h>
#import <objc/runtime.h>
@implementation VVLayout (Tracking)

+(void)load {
    Method setStringKey = class_getInstanceMethod(self, @selector(setStringData:forKey:));
    Method mkeSetStringKey =class_getInstanceMethod(self,@selector(mke_setStringData:forKey:));
    method_exchangeImplementations(setStringKey, mkeSetStringKey);
}

- (BOOL)mke_setStringData:(NSString *)data forKey:(int)key{
    BOOL ret = NO;
    if (key == STR_ID_background) {
        self.backgroundColor = [UIColor vv_colorWithString:data] ?: [UIColor blackColor];
        ret = YES;
    }
    return ret;
}

@end