8

我使用以下代码设置了 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的默认外观,如 在此处输入图像描述

那我该怎么办???

4

4 回答 4

10

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

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];
于 2013-03-22T04:08:53.493 回答
3

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

于 2013-03-22T04:04:58.110 回答
2

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

于 2013-03-22T03:41:52.857 回答
1

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

于 2013-03-22T04:09:29.333 回答