开发者社区> 问答> 正文

设置默认UISegmentedControl

试着设置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];

然后显示出的界面很漂亮:
screenshot

但是我需要的界面是这样的:

screenshot
应该怎么设置?

展开
收起
爵霸 2016-03-24 10:17:04 2043 0
1 条回答
写回答
取消 提交回答
  • 将下面这段代码放到设置默认控制器的viewcontroller 中\

    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];
    2019-07-17 19:12:05
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载