【问题标题】:how to set back default UISegmentedControl appearance?如何设置默认 UISegmentedControl 外观?
【发布时间】:2014-02-07 23:28:06
【问题描述】:

我已经使用以下代码设置了 UISegmentedControl 的外观,

UIImage *segmentSelected = [[UIImage imageNamed:@"Segment_Unselected.png"]
                            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"Segment_Selected.png"]
                              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
                                           forState:UIControlStateNormal
                                         barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
                                           forState:UIControlStateSelected
                                         barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                         [UIColor colorWithRed:77.0/255.0 green:45.0/255.0 blue:8.0/255.0 alpha:1],UITextAttributeTextColor,
                                                         [UIColor clearColor], UITextAttributeTextShadowColor,
                                                         [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset,
                                                         [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                         [UIColor whiteColor],UITextAttributeTextColor,
                                                         [UIColor clearColor], UITextAttributeTextShadowColor,
                                                         [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset,
                                                         [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateSelected];

[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"SegmentedControl_Divider.png"]
                             forLeftSegmentState:UIControlStateNormal
                               rightSegmentState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];

我得到了完美的输出

但现在我想设置 UISegment 的默认外观,例如

那我该怎么办???

【问题讨论】:

  • @BillWoodger 我只是在实现这段代码,我做了一个我实际发现的更改,它在代码中清晰可见。这似乎是一个复制粘贴错误。我知道每个尝试使用此代码的人最终都会发现错误,但我只是想节省其他人的时间:-)
  • @BillWoodger 你是对的。我应该在这里的评论中提到这个问题。错误的图像被分配给上面的segmentSelectedsegmentUnselected 变量。
  • @AdilMalik 您可以随时将其添加为自己的答案。答案比 cmets 更明显。如果您编辑问题,您可能会混淆未来的读者,他们想知道为什么答案不能反映问题(在此示例中不一定)。我建议你添加一个答案,然后我们可以删除所有这些 cmets 以保持整洁。

标签: ios iphone objective-c uisegmentedcontrol


【解决方案1】:

将此代码放在您想要使用默认控制器的视图控制器上

UIImage *segmentSelected = [[UIImage imageNamed:nil]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];
UIImage *segmentUnselected = [[UIImage imageNamed:nil]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];
    
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
                                 forState:UIControlStateNormal
                                 barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
                                 forState:UIControlStateSelected
                                 barMetrics:UIBarMetricsDefault];
        
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor colorWithRed:77.0/255.0 green:45.0/255.0 blue:8.0/255.0 alpha:1],UITextAttributeTextColor,
                                [UIColor clearColor], UITextAttributeTextShadowColor,
                                [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset,
                                [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
        
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                 [UIColor whiteColor],UITextAttributeTextColor,
                                 [UIColor clearColor],UITextAttributeTextShadowColor,      
                                 [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],UITextAttributeTextShadowOffset,
                                 [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateSelected];
            
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:nil] 
                                 forLeftSegmentState:UIControlStateNormal   
                                 rightSegmentState:UIControlStateNormal 
                                 barMetrics:UIBarMetricsDefault];

【讨论】:

    【解决方案2】:

    在上面的代码中将 imagename 设置为 nil 并将其放在您想要默认行为的视图中

    【讨论】:

      【解决方案3】:

      删除您已分配的图像的代码。

      【讨论】:

      • 但我只想对一个视图使用默认段,而对于其他我想要自定义段,我已经在应用程序的 rootviewcontroller 中返回了此代码,那么如何重新加载默认段外观?
      • 对于其他视图,保留代码原样。对于带有默认段的视图,您可以使用界面生成器或通过代码添加默认段。
      • 我已经在界面生成器中尝试了这个,它显示了默认段,但是当我运行应用程序时,它显示了我早期设置的旧外观段.. :(
      【解决方案4】:

      将段控制器的图像设置为 nil 。然后,一旦清理你的模拟器然后运行。会好的....

      【讨论】:

        猜你喜欢
        • 2010-11-04
        • 2012-02-10
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多