UIView with shadow, rounded corners and custom drawRect

前端 未结 16 1407
南旧
南旧 2020-12-02 05:22

I have to create a custom UIView that will have round corners, a border, a shadow and its drawRect() method is overridden to provide custom drawing

16条回答
  •  庸人自扰
    2020-12-02 06:10

    The solution seems much easier than the problem might suggest. I had this with one of my views and used the core part of @Hodit's answer to get it to work. This is all you need actually:

    - (void) drawRect:(CGRect)rect {
        // make sure the background is set to a transparent color using IB or code
        // e.g.: self.backgroundColor = [UIColor clearColor]; 
    
        // draw a rounded rect in the view
        [[UIColor whiteColor] setFill];
        [[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:5.0] fill];
    
        // apply shadow if you haven't already
        self.layer.masksToBounds = NO;
        self.layer.shadowColor = [[UIColor blackColor] CGColor];
        self.layer.shadowOffset = CGSizeMake(0.0,3.0);
        self.layer.shadowRadius= 1.0;
        self.layer.shadowOpacity = 0.1;
    
        // more code here
    
    }
    

    Note that this doesn't clip subviews. Anything positioned at 0,0 in the view will overlap the visible top left rounded corner.

提交回复
热议问题