Fill in a UIView with multiple iOS colors

I would like to fill the UIView Background with several colors. I want to use it as a status bar, so if 1/2 of the necessary steps are completed, the background of the UIView will be 1/2 green and 1/2 red. When the user completes more steps (say 2/3), more of the background of the UIView will turn green (in this case 2/3).

I guess I need to override

-(void) drawREct: (CGRect) rect

I guess I get a UIView, find out how big it is, and divide it into 2 rectangles, and then fill these rectangles.

Another option is to add 2 UIViews programmatically, but I'm an IB fan.

Is it possible to break the UIView as I want?

thank

+3
source share
2 answers

IB, UIView drawRect, . , , . , ( , ).

, , , drawRect, , fill , . , . , - :

-(void)drawRect:(CGRect)rect {
    CGRect upperRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height * percentDone);
    CGRect lowerRect = CGRectMake(rect.origin.x, rect.origin.y + (rect.size.height * percentDone), rect.size.width, rect.size.height *(1-percentDone));

    [[UIColor redColor] set]; 
    UIRectFill(upperRect);
    [[UIColor greenColor] set]; 
    UIRectFill(lowerRect);
}

percentDone - float (declare, property(nonatomic), synthesize), . , -

splitView.percentDone = .5;
[splitView setNeedsDisplay];

.

+9

, , . . , , . ( ), .

, @PengOne : , "progress" -drawRect: . , . , , . , . Qix- , ...; -)

+2

All Articles