ios - UISegmentedControl 在解散后更改色调颜色

标签 ios objective-c uisegmentedcontrol

我的 Controller 上有 UISegmentedControl。当我第一次打开它时,哪种色调效果很好。但是在按下后退按钮时,我将关闭 Controller 。当重新打开同一个 Controller 时,UISegmentedControl 的色调会消失。请帮忙。提前致谢。

When open first time

When reopen after dismissing

headerSegment = [[UISegmentedControl alloc] initWithItems:segmentItemsArray];
headerSegment.selectedSegmentIndex = 0;
headerSegment.apportionsSegmentWidthsByContent = YES;
headerSegment.layer.borderWidth = 0;
headerSegment.layer.masksToBounds = NO;

UIFont *font = [UIFont systemFontOfSize:16];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                       forKey:NSFontAttributeName];
[headerSegment setTitleTextAttributes:attributes
                             forState:UIControlStateNormal];

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [ApplicationColors skyBlueColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateSelected];

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [ApplicationColors segmentNormalColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateNormal];

headerSegment.layer.borderColor = [UIColor clearColor].CGColor;
headerSegment.tintColor = [UIColor whiteColor];
headerSegment.backgroundColor = [ApplicationColors lightGrayBackgroundColor ];
headerSegment.translatesAutoresizingMaskIntoConstraints = NO;
[headerSegment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];

[contentHolderView addSubview:headerSegment];

最佳答案

尝试使用以下代码,(它对我有用)

//  ViewController.m

#import "ViewController.h"

@interface ViewController () {
    UISegmentedControl * headerSegment;    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addsegmentcontrol];
}


-(void)addsegmentcontrol{


    headerSegment = [[UISegmentedControl alloc] initWithItems:@[@"One", @"Two", @"Three"]];
    headerSegment.selectedSegmentIndex = 0;
    headerSegment.apportionsSegmentWidthsByContent = YES;
    headerSegment.layer.borderWidth = 0;
    headerSegment.layer.masksToBounds = NO;

    UIFont *font = [UIFont systemFontOfSize:16];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                           forKey:NSFontAttributeName];
    [headerSegment setTitleTextAttributes:attributes
                                 forState:UIControlStateNormal];

    [headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateSelected];

    [headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateNormal];


    headerSegment.layer.borderColor = [UIColor clearColor].CGColor;
    headerSegment.tintColor = [UIColor whiteColor];
    headerSegment.backgroundColor = [UIColor lightGrayColor];
    headerSegment.translatesAutoresizingMaskIntoConstraints = YES;
    //[headerSegment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:headerSegment];
     headerSegment.frame = CGRectMake(0, 200, 320, 44);
    [self.view layoutIfNeeded];
}

这是示例结果:

enter image description here

关于ios - UISegmentedControl 在解散后更改色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49357197/

相关文章:

ios - 如何将cocos2d-x 3.1模板获取到XCode 5.x中?

iphone - 在两个 ViewController 之间共享 NSArray 数据

ios - iOS 中跨不同 Size 类别的 Segment Bar

ios - 如何使导航 Controller 导航栏不可见或至少改变颜色

带有部分的 UITableViewCell 中的 iOS 6 UITextField 在滚动时得到错误的数据

iphone - 是否有使用App Store API的官方文档?

ios - 在单个设备 (iPhone) 上运行具有相同配置文件的多个应用程序

ios - 我需要制作一个同时显示文本和图像的 UISegmentedControl

swift - 根据段显示正确的文本颜色?

ios - 如何在iOS上淡入单个文本字母