iphone - 具有方向或箭头主题的 UISegmentedControl

标签 iphone ios objective-c uisegmentedcontrol uicontrol

我已经使用 BASequenceControl 实现了带有 Direction 主题的 UISegmentedControl 来自 cocoacontrols.com

我添加了 BASequenceControl.h BASequenceControl.m 类(class)和所需图片来自 GitHub

太棒了。它对我来说工作正常..但是我对最后一段部分提示有疑问。

它显示了最后一段的垃圾空间。

原始屏幕截图

enter image description here

我需要这样

enter image description here

我实现的代码

        #import "BASequenceControl.h"

BASequenceControl *bASequenceControl = [[BASequenceControl alloc] init];                                    
bASequenceControl.frame = CGRectMake(10, 10, 200, 44);
[bASequenceControl addSegmentWithTitle:@"First" animated:NO];
[bASequenceControl addSegmentWithTitle:@"Second" animated:NO];

bASequenceControl.leftMargin = -22;
bASequenceControl.rightMargin = 0;
bASequenceControl.overlapWidth = 22;

[self.view addSubview:bASequenceControl];

感谢任何帮助。

谢谢。

最佳答案

这是一个非常简单的修复。您必须编辑 BASequenceControl.m 文件,或者您可以复制该类并重命名它。

导致问题的行在 drawRect: 中,它基本上在控件的整个背景上绘制了灰色箭头。在空白区域创建漂亮的渐变。

[passiveSegmentImage drawInRect:CGRectMake(-passiveSegmentImage.size.width, 0,
                                           w + 2 * passiveSegmentImage.size.width, h)];

您可以将其更改为:

[passiveSegmentImage drawInRect:CGRectMake(0, 0,
                                           w, h)];

现在你必须告诉控件它不应该是不透明的。像这样更新初始值设定项。

- (void)awakeFromNib {
    _selectedSegmentIndex = -1;
    [self setOpaque:NO];
    [super awakeFromNib];
}

- (id)init {
    if ((self = [super init])) {
        [self setOpaque:NO];
        _selectedSegmentIndex = -1;
    }
    return self;
}

这是非常快速和肮脏的,你可以潜在地使用一个属性来设置它。然后向 BaseAppKit 提交拉取请求,但我会把它留给你。这是一个要点,您可以将其直接复制并粘贴到 BASequenceControl.m 中以修复悬垂。 https://gist.github.com/4632686

编辑:确保您使用 init 作为初始值设定项,然后使用 setFrame:(我不太确定为什么 initWithFrame:在类里面没有被覆盖。)

BASequenceControl *control = [[BASequenceControl alloc] init];
[control setFrame:CGRectMake(0, 0, 300, 40)];

Fixed control, green for dramatic effect

戏剧效果的绿色背景

关于iphone - 具有方向或箭头主题的 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514712/

相关文章:

iphone - MFMessageComposeViewController 继续之前的对话

iphone - 带有两行标签的 UIBarButtonItem 宽度

iphone - 触摸 uiview 内部和外部按钮时触发事件

ios - FFMPEG 将 .avi 转换为可播放的 iOS 电影 mp4

ios - 在 block 内访问 BOOL

ios - 无法访问 UITableViewCell 中 UIView 的图层属性

iphone - 带选择器 View 的 UITableViewController

iphone - iPad 使用图像和 javascript 将本地 HTML 加载到 Web View 中

ios - 将 UITabBarItem 图像左右对齐

iphone - Flurry 是否按安装或用户 ID、iPhone 进行跟踪?