gpt4 book ai didi

ios - 是否可以获得导航栏当前的 "barTintColor"?

转载 作者:行者123 更新时间:2023-11-29 00:35:52 27 4
gpt4 key购买 nike

我正在尝试为我的应用程序创建一个单元测试,以检查导航栏的颜色并将其与应用程序中为主题颜色保存的用户默认值进行比较。

我似乎找不到访问条形图当前颜色的方法。这可能吗?

- (void) testColorThemeNavigationBar {
UINavigationBar *navigationBar = [[UINavigationBar alloc] init];
MSMColorManager *manager = [[MSMColorManager alloc]init];
navigationBar = [manager getNavBar];

UIColor *navBarColor = [[UIColor alloc]init];
UIColor *currentThemeColor = [[UIColor alloc] init];

navBarColor = navigationBar.barTintColor;
currentThemeColor = [manager themeColor];

XCTAssertEqualObjects(navBarColor, currentThemeColor);
}

现在我的测试失败,表明 navBarColor 的值为 null,而我的 currentThemeColor 填充了正确的 UIColorObject。我相信这是因为 barTintColor 方法仅用于设置颜色,而不是检索。

我也试过 backgroundColor 无济于事。

谢谢。

最佳答案

因为我已经在我的其中一个应用程序中使用了基于主题的设置,所以我到目前为止所做的是为了获得 navigationBar 色调颜色。

获取导航栏色调颜色:

AppDelegate *objPCAppDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;  
NSLog(@"%@",[objPCAppDelegate.window rootViewController]);

UINavigationController *navController = (UINavigationController*)[objPCAppDelegate.window rootViewController];
NSLog(@"%@",navController.navigationBar.barTintColor);

不要创建 UINavigationController 的新对象,只需从您的 application.window 中获取并在您的测试用例中使用它。

注意:如果您使用导航栏的默认颜色,它将始终返回 nil,因为默认导航栏没有颜色。

要获得颜色,您必须为导航栏设置自定义颜色,而不是在测试用例中获得它。

关于ios - 是否可以获得导航栏当前的 "barTintColor"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40645724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com