Open CherishSmile opened 4 years ago
titleColorNormal无法动态修改
暗黑模式跟作者写的组件的出发点是各自独立的,组件只是提供一个如UIPageViewController的容器,暗黑模式说白了就是换肤,找到换肤的时机就好。
腾讯开源组件QMUI给出了拦截的方法。链接
if (qmui_lastNotifiedUserInterfaceStyle != traitCollection.userInterfaceStyle) {
qmui_lastNotifiedUserInterfaceStyle = traitCollection.userInterfaceStyle;
[[NSNotificationCenter defaultCenter] postNotificationName:QMUIUserInterfaceStyleWillChangeNotification object:traitCollection];
}
@Mr-yuwei 我试过的,我监听了dark/light模式的切换通知,在那个方法里修改titleColorNormal不管用
@CherishSmile 你可以试试
暗黑模式跟作者写的组件的出发点是各自独立的,组件只是提供一个如UIPageViewController的容器,暗黑模式说白了就是换肤,找到换肤的时机就好。
腾讯开源组件QMUI给出了拦截的方法。链接
if (qmui_lastNotifiedUserInterfaceStyle != traitCollection.userInterfaceStyle) { qmui_lastNotifiedUserInterfaceStyle = traitCollection.userInterfaceStyle; [[NSNotificationCenter defaultCenter] postNotificationName:QMUIUserInterfaceStyleWillChangeNotification object:traitCollection]; }
可能我说的有点笼统,其他的属性还好,只要在traitCollectionDidChange这个方法中,重新把menu reload就可以实现适配暗黑模式。但是有一个属性progressColor,它可能对ios13的dynamic color并不支持,如果我设置的color是dynamic color,就会发生莫名奇妙的闪退,具体闪退原因我还没有深究。所以这个属性,要么设置普通的color,要么在traitCollectionDidChange这个方法中,自己判断,重新设置,重新reload。所以我说的适配暗黑模式,就是让它中的的颜色属性,不要因为dynamic color而发生闪退,如果闪退了,那就是它的bug。
@CherishSmile 我只想改titleColorNormal的颜色,但是在监听方法中只重新设置这个颜色,是不会改变的。必须得重新reload menu
@Mr-yuwei 我试过的,我监听了dark/light模式的切换通知,在那个方法里修改titleColorNormal不管用 组价本身不支持动态修改的。
WMMenuView
里面的addItems
可看到。修改之后的titleColorNormal并没有传递给WMMenuItem。- (void)addItems {
for (int i = 0; i < self.titlesCount; i++) {
CGRect frame = [self.frames[i] CGRectValue];
WMMenuItem *item = [[WMMenuItem alloc] initWithFrame:frame];
item.tag = (i + WMMENUITEM_TAG_OFFSET);
item.delegate = self;
item.text = [self.dataSource menuView:self titleAtIndex:i];
item.textAlignment = NSTextAlignmentCenter;
item.userInteractionEnabled = YES;
item.backgroundColor = [UIColor clearColor];
item.normalSize = [self sizeForState:WMMenuItemStateNormal atIndex:i];
item.selectedSize = [self sizeForState:WMMenuItemStateSelected atIndex:i];
item.normalColor = [self colorForState:WMMenuItemStateNormal atIndex:i];
item.selectedColor = [self colorForState:WMMenuItemStateSelected atIndex:i];
item.speedFactor = self.speedFactor;
[self.scrollView addSubview:item];
}
}
解决方法:<br>
1.执行WMPageController的`reload`方法,但是之前的缓存都将清除,详细见源码。
2.WMMenuView遍历子控件,然后更新。
重新
@Mr-yuwei 我试过的,我监听了dark/light模式的切换通知,在那个方法里修改titleColorNormal不管用 组价本身不支持动态修改的。
WMMenuView
里面的addItems
可看到。修改之后的titleColorNormal并没有传递给WMMenuItem。- (void)addItems { for (int i = 0; i < self.titlesCount; i++) { CGRect frame = [self.frames[i] CGRectValue]; WMMenuItem *item = [[WMMenuItem alloc] initWithFrame:frame]; item.tag = (i + WMMENUITEM_TAG_OFFSET); item.delegate = self; item.text = [self.dataSource menuView:self titleAtIndex:i]; item.textAlignment = NSTextAlignmentCenter; item.userInteractionEnabled = YES; item.backgroundColor = [UIColor clearColor]; item.normalSize = [self sizeForState:WMMenuItemStateNormal atIndex:i]; item.selectedSize = [self sizeForState:WMMenuItemStateSelected atIndex:i]; item.normalColor = [self colorForState:WMMenuItemStateNormal atIndex:i]; item.selectedColor = [self colorForState:WMMenuItemStateSelected atIndex:i]; item.speedFactor = self.speedFactor; [self.scrollView addSubview:item]; } }
解决方法:
1.执行WMPageController的
reload
方法,但是之前的缓存都将清除,详细见源码。 2.WMMenuView遍历子控件,然后更新。#define WMMENUITEM_TAG_OFFSET 6250 #define WMBADGEVIEW_TAG_OFFSET 1212 - (WMMenuItem *)itemAtIndex:(NSInteger)index { return (WMMenuItem *)[self viewWithTag:(index + WMMENUITEM_TAG_OFFSET)]; }
你这真的麻烦了,不必要这样做。只要你在设置颜色的时候,设置成dynamic color。然后在traitCollectionDidChange这个方法中执行[self.menuView reload]就行了(除了前面说的progressColor,设置成动态的话,会发生莫名的闪退)。
ZTThemeColor和ZTTextPaleGrayColor都是动态颜色。
self.titleColorSelected = ZTThemeColor;
self.titleColorNormal = ZTTextPaleGrayColor;
-(void)traitCollectionDidChange:(UITraitCollection )previousTraitCollection{ if (@available(iOS 13.0, )) { [self.menuView reload]; [self.menuView selectItemAtIndex:self.selectIndex]; } }
-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
但是这样每个页面都会重新加载
-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
但是这样每个页面都会重新加载
看清楚呀,不是controller reload,是controller.menuView reload;
-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
但是这样每个页面都会重新加载
看清楚呀,不是controller reload,是controller.menuView reload;
看错了。。
请适配ios13暗黑模式
请适配ios13暗黑模式