如何更改 viewDidLoad 上选定段索引的色调? (基于教程)

发布于 2025-01-02 11:04:45 字数 1599 浏览 0 评论 0原文

我按照以下教程更改分段控件的颜色: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿得七秒忆 2025-01-09 11:04:45

从 swift 3 开始,将此代码放入 viewDidLoad()ViewDidAppear()

//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)

As of swift 3 put this code inside viewDidLoad() or ViewDidAppear()

//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)
逆夏时光 2025-01-09 11:04:45

在 -viewDidLoad 中,尝试调用 [self seleccionarSegmented:miSegmentedControl] (希望我的名字是正确的。

In -viewDidLoad, try calling [self seleccionarSegmented:miSegmentedControl] (hopefully I've got the names right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文