简体   繁体   中英

Shadow with Corner radius in cell with IBDesignables

I have a 2 sub views in cell. One for shadow view and another for corner radius as it sounds a good approach that I found from this link . So I tried to make it in Objective-C with IBDessignables. Everything works fine if the cell is of same size but for a dynamic size shadow is showing over another cell but the cornerview is fine.

-- View hierarchy of cell --

-- UITableViewCell
  -- contentView
     -- shadowView
       -- CornerView

Here is my code for shadow View

ShadowView.h

IB_DESIGNABLE
@interface ShadowView : UIView
@property (nonatomic) IBInspectable CGFloat cornerRadius;
@property (nonatomic) IBInspectable CGFloat shadowRadius;
@property (nonatomic) IBInspectable CGSize  shadowOffset;
@end

ShadowView.m

@implementation ShadowView

-(void)setup{
    self.cornerRadius = 5.0f;
    self.shadowRadius = 2.0f;
    self.shadowOffset  = CGSizeMake(0, 0);
    self.backgroundColor = [UIColor clearColor];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [self updateLayerProperties];
}

- (void)updateLayerProperties {
    self.layer.shadowOffset = self.shadowOffset;
    self.layer.shadowRadius = self.shadowRadius;
    self.layer.shadowOpacity = 0.3;
    self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.cornerRadius].CGPath;
    self.layer.masksToBounds = NO;
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

@end

So does anyone know what the exact issue is? Because the corner view bounds work perfectly but not the shadow view.

Demo File

And here is the demo file if anybody wants to test it .. https://www.dropbox.com/s/myxjyj5pu3ey3aw/demoDesignables.zip?dl=0

Views are responsible for drawing content, handling multitouch events, and managing the layout of any subviews. So, you need to override layoutSubviews function of your ShadowView .

-(void)layoutSubviews {
    [super layoutSubviews];
    [self updateLayerProperties];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM