mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fix(ios): apply proper border radius to box shadow and view sublayers (#9881)
This commit is contained in:
committed by
GitHub
parent
6e3dd4da51
commit
3d882b0999
@@ -712,6 +712,7 @@ function drawNoRadiusNonUniformBorders(nativeView: NativeScriptUIView, backgroun
|
|||||||
// TODO: use sublayer if its applied to a layout
|
// TODO: use sublayer if its applied to a layout
|
||||||
function drawBoxShadow(nativeView: NativeScriptUIView, view: View, boxShadow: CSSShadow, background: BackgroundDefinition, useSubLayer: boolean = false) {
|
function drawBoxShadow(nativeView: NativeScriptUIView, view: View, boxShadow: CSSShadow, background: BackgroundDefinition, useSubLayer: boolean = false) {
|
||||||
const layer: CALayer = iOSNativeHelper.getShadowLayer(nativeView, 'ns-box-shadow');
|
const layer: CALayer = iOSNativeHelper.getShadowLayer(nativeView, 'ns-box-shadow');
|
||||||
|
const renderSize = view.getActualSize() || { width: 0, height: 0 };
|
||||||
|
|
||||||
layer.masksToBounds = false;
|
layer.masksToBounds = false;
|
||||||
nativeView.clipsToBounds = false;
|
nativeView.clipsToBounds = false;
|
||||||
@@ -720,10 +721,23 @@ function drawBoxShadow(nativeView: NativeScriptUIView, view: View, boxShadow: CS
|
|||||||
// nativeView.clipsToBounds doesn't work
|
// nativeView.clipsToBounds doesn't work
|
||||||
view.setProperty('clipToBounds', false);
|
view.setProperty('clipToBounds', false);
|
||||||
|
|
||||||
|
// This should match the view's border radius (only uniform radius is supported for now)
|
||||||
|
let cornerRadius = layout.toDeviceIndependentPixels(background.borderTopLeftRadius);
|
||||||
|
cornerRadius = Math.min(Math.min(renderSize.width / 2, renderSize.height / 2), cornerRadius);
|
||||||
|
|
||||||
|
// Apply corner radius to sub layers as clipToBounds and masksToBounds are now set to false
|
||||||
|
const sublayers = nativeView.layer?.sublayers;
|
||||||
|
if (sublayers) {
|
||||||
|
for (let i = 0; i < sublayers.count; i++) {
|
||||||
|
sublayers.objectAtIndex(i).cornerRadius = cornerRadius;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!background.color?.a) {
|
if (!background.color?.a) {
|
||||||
// add white background if view has a transparent background
|
// add white background if view has a transparent background
|
||||||
layer.backgroundColor = UIColor.whiteColor.CGColor;
|
layer.backgroundColor = UIColor.whiteColor.CGColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
// shadow opacity is handled on the shadow's color instance
|
// shadow opacity is handled on the shadow's color instance
|
||||||
layer.shadowOpacity = boxShadow.color?.a ? boxShadow.color?.a / 255 : 1;
|
layer.shadowOpacity = boxShadow.color?.a ? boxShadow.color?.a / 255 : 1;
|
||||||
layer.shadowRadius = Length.toDevicePixels(boxShadow.blurRadius, 0.0);
|
layer.shadowRadius = Length.toDevicePixels(boxShadow.blurRadius, 0.0);
|
||||||
@@ -735,9 +749,6 @@ function drawBoxShadow(nativeView: NativeScriptUIView, view: View, boxShadow: CS
|
|||||||
Length.toDevicePixels(boxShadow.offsetY, 0.0)
|
Length.toDevicePixels(boxShadow.offsetY, 0.0)
|
||||||
);
|
);
|
||||||
|
|
||||||
// this should match the view's border radius
|
|
||||||
const cornerRadius = Length.toDevicePixels(<CoreTypes.LengthType>view.style.borderRadius, 0.0);
|
|
||||||
|
|
||||||
// apply spreadRadius by expanding shadow layer bounds
|
// apply spreadRadius by expanding shadow layer bounds
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
const bounds = CGRectInset(nativeView.bounds,
|
const bounds = CGRectInset(nativeView.bounds,
|
||||||
@@ -751,10 +762,18 @@ function drawBoxShadow(nativeView: NativeScriptUIView, view: View, boxShadow: CS
|
|||||||
|
|
||||||
function clearBoxShadow(nativeView: NativeScriptUIView) {
|
function clearBoxShadow(nativeView: NativeScriptUIView) {
|
||||||
nativeView.clipsToBounds = true;
|
nativeView.clipsToBounds = true;
|
||||||
|
const sublayers = nativeView.layer?.sublayers;
|
||||||
|
if (sublayers) {
|
||||||
|
for (let i = 0; i < sublayers.count; i++) {
|
||||||
|
sublayers.objectAtIndex(i).cornerRadius = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const layer: CALayer = iOSNativeHelper.getShadowLayer(nativeView, 'ns-box-shadow', false);
|
const layer: CALayer = iOSNativeHelper.getShadowLayer(nativeView, 'ns-box-shadow', false);
|
||||||
if (!layer) {
|
if (!layer) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
layer.masksToBounds = true;
|
layer.masksToBounds = true;
|
||||||
layer.shadowOffset = CGSizeMake(0, 0);
|
layer.shadowOffset = CGSizeMake(0, 0);
|
||||||
layer.shadowColor = UIColor.clearColor.CGColor;
|
layer.shadowColor = UIColor.clearColor.CGColor;
|
||||||
|
|||||||
Reference in New Issue
Block a user