这不是错误。全局着色适用于对象,其类用于着色。如果为一个类声明了特定的色调方案,那么属于该类的所有控件都将遵循该类的色调方案,而不管它们被添加到哪个视图。
我已经尝试过实现这个&我在这里使用这个代码。
在appDelegate 中为UINavigationBar 设置全局着色
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
...
...
}
我在UINavigationBar 中添加UISegmentControl 以及viewDidLoad 方法中的tableView.tableHeaderView。
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"One", @"Two"]];
segment.frame = CGRectMake(self.view.frame.size.width/2-75, 0, 150, 40);
[self.navigationController.navigationBar addSubview:segment];
UISegmentedControl *tableSegment = [[UISegmentedControl alloc]initWithItems:@[@"One", @"Two"]];
tableSegment.frame = CGRectMake(self.view.frame.size.width/2-75, 0, 150, 40);
[self.navigationController.navigationBar addSubview:tableSegment];
tableView.tableHeaderView = tableSegment;
这会产生以下结果。如果UISegmentControl 作为子视图添加到UINavigationBar 容器中,则UISegmentControl 将使用UINavigationBar 的全局色调进行着色,否则,它将采用UINavigationBar 之外的默认蓝色,就像UITableView 的标题中一样
现在为UISegmentedControl 添加一个特定的色调
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
[[UISegmentedControl appearance] setTintColor:[UIColor brownColor]];
...
...
}
``
它产生了这个,UISegmentControl 现在受到其全局色调的影响,无论其位置如何(如前面的UINavigationBar 和UITableViewHeader)。这是
但是UInavigationBar 的 TinitColor 仍然适用于 leftBarButtonItem,因为没有为 UIBarButtonItem 声明全局色调方案,因此它采用了它添加到的容器的 tintColor。
为 Clarify 添加更多代码
- 对
UISegmentedControl 使用全局着色,而不使用其他全局着色。
[[UISegmentedControl appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
结果:您可以看到段控件,但请注意 backButton,它的默认色调为 UINavigationBar
- 为
UISegmentedControl 和UINavigationBar 使用全局色调
[[UISegmentedControl appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
结果:您可以看到段控件色调,还可以看到 backButton,它的全局色调为 UINavigationBar
我还做了UISegmentedControl 的默认选择。如果您使用此代码得到任何其他结果,那么您的代码中肯定发生了一些额外的事情。
我希望这会有所帮助。