Set background color for only part of UIView

前端 未结 6 2670
生来不讨喜
生来不讨喜 2021-02-20 09:33

I want the bottom (not quite half) of my UIView to be a different color than the top.

I\'m wondering if I should create a CGRect and then color that? Is this along the r

6条回答
  •  -上瘾入骨i
    2021-02-20 10:10

    Yes, as you are already overriding drawRect method, this will do.

    - (void)drawRect:(CGRect)rect { 
    
        CGRect topRect = CGRectMake(0, 0, rect.size.width, rect.size.height/2.0);
        // Fill the rectangle with grey
        [[UIColor greyColor] setFill];
        UIRectFill( topRect );
    
        CGRect bottomRect = CGRectMake(0, rect.size.height/2.0, rect.size.width, rect.size.height/2.0);
        [[UIColor redColor] setFill];
        UIRectFill( bottomRect );
    
    }
    

    Change the values inside the frames as you wish.

提交回复
热议问题