Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

iphone - UISegmentedControl with Direction or Arrow theme

I have implemented UISegmentedControl with Direction theme using BASequenceControl from cocoacontrols.com.

I have added BASequenceControl.h and BASequenceControl.m classes and required images from GitHub

Great. Its working fine for me..However I have a concern with the last segment section tip.

Its displaying the junk space of last segment section.

Original Screen Shot

enter image description here

I need like this

enter image description here

The code I have Implemented

        #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];

Any help on this is appreciated.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a pretty simple fix. You will have to edit the BASequenceControl.m file or you can duplicate the class and rename it.

The line that is causing the problem is in drawRect: it basically draws the grey arrow across the entire background of the control. Creating that nice gradient in the empty space.

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

You can change it to:

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

Now you have to tell the control that it should not be opaque. Update the initializers like this.

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

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

This is pretty quick and dirty, you could potentially make this settable with a property. Then submit a pull request to BaseAppKit, but I'll leave that to you. Here is a gist that you can copy and paste directly in BASequenceControl.m to fix the overhang. https://gist.github.com/4632686

Edit: Make sure that you are using init as the initializer and then setFrame: (I'm not really sure why initWithFrame: wasn't overridden in the class.)

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

Fixed control, green for dramatic effect

Green background for dramatic effect


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...