如何更改 viewDidLoad 上选定段索引的色调? (基于教程)
我按照以下教程更改分段控件的颜色: http://goddess-gate.com/dc2/index.php/post/454
但是,我很难理解代码并使用选定的段加载视图。换句话说,我已设法将分段控件的颜色更改为黑色,但只有当我选择分段时才会发生这种情况,而不是在加载视图时发生。当我加载视图时,它仅以灰色显示。另外,如果我想重置为原始灰色或更改视图中出现的分段控件的颜色,如何消除黑色并返回到灰色且未选中的所有内容?
有人可以指出我需要添加到 viewDidLoad 或 viewWillAppear 方法中的具体内容和原因吗?
这也是代码:
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;
}
和操作方法
-(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];
}
}
I followed the following tutorial to change the color of a segmented control:
http://goddess-gate.com/dc2/index.php/post/454
However, I am struggling to understand the code and load the view with a selected segment. On other words, I have managed to change the color of my segmented control to black, but it only happens when I select a segment and not when loading the view. When I load the view, it only appears in a color gray. Also, if I want to reset to the original gray color or change the color of the segmented Control in view will appear, how can I eliminate the black color and return to everything in gray and not selected?
Can someone point me out what exactly and why, do I need to add to the viewDidLoad or viewWillAppear method?
Here is also the code:
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;
}
And the Action Method
-(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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 swift 3 开始,将此代码放入
viewDidLoad()
或ViewDidAppear()
中As of swift 3 put this code inside
viewDidLoad()
orViewDidAppear()
在 -viewDidLoad 中,尝试调用
[self seleccionarSegmented:miSegmentedControl]
(希望我的名字是正确的。In -viewDidLoad, try calling
[self seleccionarSegmented:miSegmentedControl]
(hopefully I've got the names right.