在一个自定义的 tableview 单元格中,我正在绘制一个带有阴影的简单矩形,如下所示:
photoBorder = [[[UIView alloc] initWithFrame:CGRectMake(4, 4, self.frame.size.width-8, 190)] autorelease];
photoBorder.autoresizingMask = UIViewAutoresizingFlexibleWidth;
photoBorder.backgroundColor = [UIColor whiteColor];
photoBorder.layer.masksToBounds = NO;
photoBorder.layer.shadowOffset = CGSizeMake(0, 1);
photoBorder.layer.shadowRadius = 4;
photoBorder.layer.shadowOpacity = 1.0;
photoBorder.layer.shadowColor = [UIColor darkGrayColor].CGColor;
photoBorder.layer.shouldRasterize = YES;
photoBorder.layer.shadowPath = [UIBezierPath bezierPathWithRect:photoBorder.bounds].CGPath; // this line seems to be causing the problem
这在 View 首次加载时工作正常。但是,当您旋转设备时,阴影会保持相同大小。我真的很希望它能拉伸(stretch)到“photoBorder”的新宽度。
我可以通过删除 shadowPath 让它工作,但是 tableview 的性能受到了明显的影响。
有人有关于在 UIView 上制作阴影的任何提示,该阴影可以拉伸(stretch)而不损失性能吗?
最佳答案
在搜索了几个小时但没有找到任何东西后,我发布了这个。然后几分钟后找到答案。
对我来说,简单的解决方案似乎只是将 shadowPath 移动到 layoutSubviews 中。
- (void)layoutSubviews{
photoBorder.layer.shadowPath = [UIBezierPath bezierPathWithRect:photoBorder.bounds].CGPath;
}
关于ios - 设备旋转时如何使 UIView 阴影拉伸(stretch)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13366784/