更改UISegmentedControl for iPad的文本颜色

时间:2015-04-21 08:51:03

标签: ios objective-c xcode

我是Objective C的新手,只需将UISegmentedControl的文本颜色更改为白色,色调颜色为黑色不透明度减少。然而,这适用于iPhone,但在iPad上,文字颜色不正确。它的字体也需要色调颜色。 以下是我使用的代码。

[ segCtrl setTintColor:[ UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.15 ] ];

[ segCtrl setSelectedSegmentIndex:0 ];

[ segCtrl setTitleTextAttributes:@{NSFontAttributeName:[ UIFont fontWithName:FONTNAME size:13.0 ], NSForegroundColorAttributeName:[UIColor whiteColor ]} forState:UIControlStateNormal ];

我该如何解决这个问题。为什么两个设备中相同的代码都不同。

以下是我从iPhone和iPad获得的图片

iPhone enter image description here

2 个答案:

答案 0 :(得分:1)

使用此代码: UISegmentedControl_Class

进入viewDidLoad函数。

// color selected text ---> red
    [[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateSelected];

    // color disabled text ---> blue
    [[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];

    // color tint segmented control ---> Your color
    [[UISegmentedControl appearance] setTintColor:[ UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.15 ]];

答案 1 :(得分:1)

将以下代码放在viewdidload:

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateNormal]; 

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateSelected];

为我工作。