【问题标题】:UISegmented Control Global Tint in Navigation Controller Header导航控制器标题中的 UISegmented 控件全局色调
【发布时间】:2014-01-19 07:57:19
【问题描述】:

在我的表格视图标题中设置全局色调没有按预期进行着色,并且似乎是一个错误。

当我加载视图时,我得到了

右侧保留默认颜色,但是如果我按下右侧按钮并再次按下左侧按钮,它会设置为您期望的颜色:

这个问题只出现在我的导航栏中的分段控件上。当控件嵌入“普通”视图时,全局着色起作用。

我有一个解决问题的方法。因为,我在viewDidLoad中设置了右边的控件,然后回到左边。

这是一个错误吗?还是我错过了什么?谢谢。

添加:我正在故事板中设置全局色调

【问题讨论】:

  • 愿意解释否决票吗?

标签: ios7 uisegmentedcontrol


【解决方案1】:

这不是错误。全局着色适用于对象,其类用于着色。如果为一个类声明了特定的色调方案,那么属于该类的所有控件都将遵循该类的色调方案,而不管它们被添加到哪个视图。

我已经尝试过实现这个&我在这里使用这个代码。

在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 的默认选择。如果您使用此代码得到任何其他结果,那么您的代码中肯定发生了一些额外的事情。

我希望这会有所帮助。

【讨论】:

  • 谢谢,但为什么我的一半设置控件设置为全局色调?整个事情应该设置为全局色调。为什么一旦我点击它,它就会变成全局色调。
  • 请参考添加的代码作为答案的更新。我猜你的代码中有一些额外的东西会造成一些冲突。那么请分享您的代码。
  • 我会尽快检查我的应用程序,以便您知道我在情节提要中设置全局色调。
  • 抱歉 Smick,我不太熟悉 StoryBoards。欢迎任何进一步的代码。
【解决方案2】:

我最近在 Xcode 5.0.2 中遇到了一个大问题。当tintColor 的继承突然停止正常工作时,我正在为各种项目使用 Global Tint 和 Tint。我最终手动清理了 Storyboard 的 XML 文件以解决问题。

如果您没有在您的应用程序中使用任何 UIAppearance 魔法,那在我看来肯定是一个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-30
    • 2011-04-19
    • 2021-12-20
    • 2017-05-14
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多