Ios 如何更改viewDidLoad上选定段索引的色调?(基于教程)
我按照以下教程更改分段控件的颜色: 但是,我很难理解代码并加载带有选定段的视图。换句话说,我已经成功地将分段控件的颜色更改为黑色,但它仅在选择分段时发生,而不是在加载视图时发生。加载视图时,它仅以灰色显示。此外,如果我想重置为原始灰色或更改视图中分段控件的颜色,如何消除黑色并返回灰色和未选中的所有内容 有人能告诉我需要添加到viewDidLoad或ViewWillAspect方法的确切内容和原因吗 代码如下:Ios 如何更改viewDidLoad上选定段索引的色调?(基于教程),ios,subview,uisegmentedcontrol,Ios,Subview,Uisegmentedcontrol,我按照以下教程更改分段控件的颜色: 但是,我很难理解代码并加载带有选定段的视图。换句话说,我已经成功地将分段控件的颜色更改为黑色,但它仅在选择分段时发生,而不是在加载视图时发生。加载视图时,它仅以灰色显示。此外,如果我想重置为原始灰色或更改视图中分段控件的颜色,如何消除黑色并返回灰色和未选中的所有内容 有人能告诉我需要添加到viewDidLoad或ViewWillAspect方法的确切内容和原因吗 代码如下: NSInteger static compareViewsByOrigin(id s
NSInteger static compareViewsByOrigin(id sp1, id sp2, void *context)
{
// UISegmentedControl segments use UISegment objects (private API). Then we can safely
// cast them to UIView objects.
float v1 = ((UIView *)sp1).frame.origin.x;
float v2 = ((UIView *)sp2).frame.origin.x;
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
NSInteger静态CompareViewByOrigin(id sp1、id sp2、void*上下文)
{
//UISegmentedControl段使用UISegment对象(私有API),这样我们就可以安全地
//将它们强制转换为UIView对象。
float v1=((UIView*)sp1.frame.origin.x;
float v2=((UIView*)sp2.frame.origin.x;
如果(v1v2)
退而求其次;
其他的
返回指定名称;
}
以及行动方法
-(IBAction)seleccionarSegmented:(id)sender {
int numSegments = [miSegmentedControl.subviews count];
for( int i = 0; i < numSegments; i++ ) {
[[miSegmentedControl.subviews objectAtIndex:i] setTintColor:nil];
[[miSegmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1]];
}
NSArray *sortedViews = [miSegmentedControl.subviews sortedArrayUsingFunction:compareViewsByOrigin context:NULL];
[[sortedViews objectAtIndex:miSegmentedControl.selectedSegmentIndex] setTintColor: [UIColor blackColor]];
for (id view in miSegmentedControl.subviews) {
[view removeFromSuperview];
}
for (id view in sortedViews) {
[miSegmentedControl addSubview:view];
}
}
-(iAction)选择分类分段:(id)发送方{
int numSegments=[miSegmentedControl.subviews count];
对于(int i=0;i
在-viewDidLoad中,尝试调用[self-selectionarsegmented:misesegmentedcontrol]
(希望我的名字是对的。在-viewDidLoad中,尝试调用[self-selectionarsegmented:misesegmentedcontrol]
(希望我的名字是对的。从swift 3开始,将这段代码放入viewDidLoad()中)
或视图显示()
从swift 3开始,将此代码放入viewDidLoad()
或viewdidappease()
//this sets the value of the index that you want to call
let segmentedControlIBOutlet = 0
//this calls the actions for that index
segmentedControlAction(segmentedControlIBOutlet as AnyObject)