【发布时间】:2015-02-19 05:02:09
【问题描述】:
当颜色不同时,UISegmentControl 的边缘看起来像这样很烦人:
有没有办法移除或隐藏颜色或重塑 UISegment 以掩盖它?
第二个问题是如何在从开/关/其他切换时以编程方式更改颜色?
注意:我使用了“回答你自己的问题”选项,因为我找不到从角落边缘移除颜色的答案,并以这种方式解决了我的问题。
【问题讨论】:
标签: ios objective-c uisegmentedcontrol uiswitch
当颜色不同时,UISegmentControl 的边缘看起来像这样很烦人:
有没有办法移除或隐藏颜色或重塑 UISegment 以掩盖它?
第二个问题是如何在从开/关/其他切换时以编程方式更改颜色?
注意:我使用了“回答你自己的问题”选项,因为我找不到从角落边缘移除颜色的答案,并以这种方式解决了我的问题。
【问题讨论】:
标签: ios objective-c uisegmentedcontrol uiswitch
要更改 UISwitch 的颜色,您可以在 viewDidLoad 方法中使用以下内容:
//Custom colour
UIColor * customColor = [UIColor colorWithRed: 255/255.0f green: 239/255.0f blue: 54/255.0f alpha: 1.0f];
//sets custom colour tint of selected
[self.yourSwitch setTintColor:customColor];
//sets background colour using default UIColor option
[self.yourSwitch setBackgroundColor:[UIColor blackColor]];
这将给出与问题相同的外观。
要删除边缘,请在 viewDidLoad 方法中使用此代码:
//this sets a border with a rounded edge so the
self.yourSwitch.layer.borderWidth = 1.2f;
self.yourSwitch.layer.cornerRadius = 6.0;
要在不同位置(开/关/其他)改变颜色,请使用以下命令:
- (IBAction)yourSwitchSelection:(UISegmentedControl *)sender {
if (self.yourSwitch.selectedSegmentIndex == 1){
UIColor * customColor = [UIColor colorWithRed: 255/255.0f green: 112/255.0f blue: 0/255.0f alpha: 1.0f];
[self.yourSwitch setTintColor:customColor];
}
if (self.yourSwitch.selectedSegmentIndex == 0){
UIColor * customColor = [UIColor colorWithRed: 255/255.0f green: 239/255.0f blue: 54/255.0f alpha: 1.0f];
[self.yourSwitch setTintColor:customColor];
}
}
现在它将在选择索引0时看起来像这样:
选择索引1时,如此:【讨论】: