我的屏幕上有这个:
使用此代码 sn-p:
[_backgroundBoxWithShadow.layer setOpacity:0.4];
[_backgroundBoxWithShadow setBackgroundColor:[UIColor whiteColor]];
[_backgroundBoxWithShadow.layer setShadowOpacity:1.0];
[_backgroundBoxWithShadow.layer setShadowColor:[[UIColor blackColor] CGColor]];
[_backgroundBoxWithShadow.layer setShadowOffset:CGSizeMake(0.0, 0.0)];
[_backgroundBoxWithShadow.layer setShadowRadius:8.0];
[_imaginaryTextBoxWithShadow.layer setShadowColor:[[UIColor blackColor] CGColor]];
[_imaginaryTextBoxWithShadow.layer setShadowOffset:CGSizeMake(-4.0, 4.0)];
[_imaginaryTextBoxWithShadow.layer setShadowRadius:4.75];
[_imaginaryTextBoxWithShadow.layer setShadowOpacity:0.4];
注意:没有一个视图包含另一个视图,它们是同一个superview 中的兄弟姐妹。但是,在我看来,结果看起来与原始屏幕截图相同 - 您仍然可以使用这些值来优化结果以满足您的最终愿望。
更新
你根据我的回答发布了你的代码片段 “不工作”:
CGFloat screenWidth = self.view.bounds.size.width;
CGFloat screenHeight = self.view.bounds.size.height;
// BEGIN - your code
UIImageView *imag = (UIImageView *)[self.view viewWithTag:1]; // the line is pointless here, anyway...
imag = [[UIImageView alloc] initWithFrame:CGRectMake((screenWidth/100)*12, (screenHeight/100)*10, (screenWidth/100)*75, (screenHeight/100)*61)];
[imag setBackgroundColor:[UIColor whiteColor]];
[imag.layer setOpacity:0.4];
[imag.layer setShadowOpacity:1.0];
[imag.layer setShadowColor:[[UIColor blackColor] CGColor]];
[imag.layer setShadowOffset:CGSizeMake(0.0, 0.0)];
[imag.layer setShadowRadius:8.0];
// END - your code
[self.view addSubview:imag];
这就是它在我身边的 真实 iPhone5 上的样子,它看起来工作得很好:
如果您对此有所不同,请发布有关它的屏幕截图(!)。
注意:必须同时保留视图的clipsToBounds 和图层的masksToBounds FALSE,否则阴影将被切断。
在Interface Builder中看起来是正确的:
或者您可以将其显式添加到您的代码中:
[imag setClipsToBounds:FALSE];
[imag.layer setMasksToBounds:FALSE];
iOS6.1 更新
最终证明,您需要一个适用于 iOS6 的解决方案,我发现该解决方案在我的 iOS6.1 设备上也能正常工作:
[_backgroundBoxWithShadow.layer setOpacity:0.4];
[_backgroundBoxWithShadow setBackgroundColor:[UIColor whiteColor]];
[_backgroundBoxWithShadow.layer setShouldRasterize:TRUE];
[_backgroundBoxWithShadow.layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[_backgroundBoxWithShadow.layer setShadowOpacity:1.0];
[_backgroundBoxWithShadow.layer setShadowColor:[[UIColor blackColor] CGColor]];
[_backgroundBoxWithShadow.layer setShadowOffset:CGSizeMake(0.0, 0.0)];
[_backgroundBoxWithShadow.layer setShadowRadius:8.0];
您也可能看到,iOS6.1 上的最终结果如下所示: