UISegmentedControl 错误的自定义处理 [英] UISegmentedControl wrong customization handling

查看:31
本文介绍了UISegmentedControl 错误的自定义处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码自定义了 UISegmentedControl 的外观:

I customized the appearance of a UISegmentedControl using the following code:

UIImage *bg = [UIImage imageNamed:@"segment_bg"];
UIImage *bg_sel = [UIImage imageNamed:@"segment_sel"];
UIImage *leftSep = [UIImage imageNamed:@"segment_div_sx"];
UIImage *rightSep = [UIImage imageNamed:@"segment_div_dx"];

UIEdgeInsets selectionInsets = UIEdgeInsetsMake(0, 8, 0, 8);

[self.segmentedControl setBackgroundImage:[bg resizableImageWithCapInsets:UIEdgeInsetsZero]
                        forState:UIControlStateNormal
                      barMetrics:UIBarMetricsDefault];

[self.segmentedControl setBackgroundImage:[bg_sel resizableImageWithCapInsets:selectionInsets]
                        forState:UIControlStateSelected
                      barMetrics:UIBarMetricsDefault];

[self.segmentedControl setBackgroundImage:[bg resizableImageWithCapInsets:UIEdgeInsetsZero]
                        forState:UIControlStateNormal
                      barMetrics:UIBarMetricsDefault];

[self.segmentedControl setDividerImage:[bg resizableImageWithCapInsets:UIEdgeInsetsZero]
          forLeftSegmentState:UIControlStateNormal
            rightSegmentState:UIControlStateNormal
                   barMetrics:UIBarMetricsDefault];

[self.segmentedControl setDividerImage:rightSep
          forLeftSegmentState:UIControlStateSelected
            rightSegmentState:UIControlStateNormal
                   barMetrics:UIBarMetricsDefault];

[self.segmentedControl setDividerImage:leftSep
          forLeftSegmentState:UIControlStateNormal
            rightSegmentState:UIControlStateSelected
                   barMetrics:UIBarMetricsDefault];

一切看起来都很好,如下图所示,切换时其他部分也很好:

Everything looks fine as you can see in the following image, also other segments look good when switching:

但是当我从第三段切换到第一段时,段分隔符是错误的(右端没有圆角,所以不是正确的分隔符).

but when I switch from the third segment to the first, the segment divider is wrong (no rounded corners on the right end, so not the correct divider).

如果分段控件有更多段,这总是(并且仅)在从第三段切换到第一段时发生.

If the segmented control has more segments, this happens always (and only) when switching from the third segment to the first.

我发现在 valueChanged: 事件纠正显示后,将 setNeedsLayout 消息发送到 UISegmentedControl 对象.

I found out that sending the setNeedsLayout message to the UISegmentedControl object after the valueChanged: event corrects the display.

现在,我自定义分段控件的方式是否有问题,是已知错误还是我应该将其报告为一个错误?

Now, is it something wrong with the way I customized the segmented control, is it a known bug or should I report this as one?

推荐答案

有点丑陋的解决方法,但我设法使用以下方法修复它,直到 Apple 自行修复.

A bit of an ugly workaround but i managed to fix it with the following until apple fixes it itself.

首先你需要继承 UISegmentedControl 并添加以下内容:

First you need to subclass UISegmentedControl and add the following:

@implementation MJSegmentedControl

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger cachedIndex = self.selectedSegmentIndex;
    self.selectedSegmentIndex = 0;
    self.selectedSegmentIndex = cachedIndex;
}

@end

这篇关于UISegmentedControl 错误的自定义处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆