Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请适配ios13暗黑模式 #622

Open
CherishSmile opened this issue Oct 17, 2019 · 12 comments
Open

请适配ios13暗黑模式 #622

CherishSmile opened this issue Oct 17, 2019 · 12 comments

Comments

@CherishSmile
Copy link

请适配ios13暗黑模式

@kaioser
Copy link

kaioser commented Nov 5, 2019

titleColorNormal无法动态修改

@Mr-yuwei
Copy link

Mr-yuwei commented Nov 28, 2019

暗黑模式跟作者写的组件的出发点是各自独立的,组件只是提供一个如UIPageViewController的容器,暗黑模式说白了就是换肤,找到换肤的时机就好。

腾讯开源组件QMUI给出了拦截的方法。链接

if (qmui_lastNotifiedUserInterfaceStyle != traitCollection.userInterfaceStyle) {
                  qmui_lastNotifiedUserInterfaceStyle = traitCollection.userInterfaceStyle;
                    [[NSNotificationCenter defaultCenter] postNotificationName:QMUIUserInterfaceStyleWillChangeNotification object:traitCollection];
     }               

@kaioser
Copy link

kaioser commented Nov 28, 2019

@Mr-yuwei 我试过的,我监听了dark/light模式的切换通知,在那个方法里修改titleColorNormal不管用

@kaioser
Copy link

kaioser commented Nov 28, 2019

@CherishSmile 你可以试试

@CherishSmile
Copy link
Author

暗黑模式跟作者写的组件的出发点是各自独立的,组件只是提供一个如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。

@kaioser
Copy link

kaioser commented Nov 28, 2019

@CherishSmile 我只想改titleColorNormal的颜色,但是在监听方法中只重新设置这个颜色,是不会改变的。必须得重新reload menu

@Mr-yuwei
Copy link

Mr-yuwei commented Nov 28, 2019

@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)];
}

@CherishSmile
Copy link
Author

重新

@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];
}
}

@kaioser
Copy link

kaioser commented Nov 28, 2019

-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{

但是这样每个页面都会重新加载

@CherishSmile
Copy link
Author

-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{

但是这样每个页面都会重新加载

看清楚呀,不是controller reload,是controller.menuView reload;

@kaioser
Copy link

kaioser commented Nov 28, 2019

-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{

但是这样每个页面都会重新加载

看清楚呀,不是controller reload,是controller.menuView reload;

看错了。。

@cbhy1988love
Copy link

请适配ios13暗黑模式

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants