ios – 如何设置默认的UISegmentedControl外观?

前端之家收集整理的这篇文章主要介绍了ios – 如何设置默认的UISegmentedControl外观?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码设置了UISegmentedControl的外观,
UIImage *segmentSelected = [[UIImage imageNamed:@"Segment_Unselected.png"]
                            resizableImageWithCapInsets:UIEdgeInsetsMake(0,12,12)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"Segment_Selected.png"]
                              resizableImageWithCapInsets:UIEdgeInsetsMake(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],nil] forState:UIControlStateSelected];

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

我得到了完美的产出

但是现在我想设置UISegment的默认外观

所以我要做什么?

解决方法

将此代码放在您使用默认控制器的视图控制器上
UIImage *segmentSelected = [[UIImage imageNamed:nil]resizableImageWithCapInsets:UIEdgeInsetsMake(0,12)];
UIImage *segmentUnselected = [[UIImage imageNamed:nil]resizableImageWithCapInsets:UIEdgeInsetsMake(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],nil] forState:UIControlStateNormal];
      
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                 [UIColor whiteColor],nil] forState:UIControlStateSelected];
          
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:nil] 
                                 forLeftSegmentState:UIControlStateNormal   
                                 rightSegmentState:UIControlStateNormal 
                                 barMetrics:UIBarMetricsDefault];
原文链接:https://www.f2er.com/iOS/337287.html

猜你在找的iOS相关文章