Xamarin.Forms中iOS端SwipeView边角显示异常问题求助
Xamarin.Forms SwipeView iOS平台边角显示异常问题
使用SwipeView组件时,仅iOS平台会出现边角显示问题,Android平台无异常。
界面效果截图:
相关代码:
<SwipeView BackgroundColor="Transparent"> <SwipeView.RightItems> <SwipeItems Mode="Reveal"> <!-- SwipeItems 相关代码 --> </SwipeItems> </SwipeView.RightItems> <Frame Margin="20" HeightRequest="65" BorderColor="CornflowerBlue" BackgroundColor="CadetBlue" CornerRadius="20"> <!-- Frame 内部代码 --> </Frame> </SwipeView>
问题原因
这是Xamarin.Forms在iOS平台的SwipeView渲染机制问题:iOS端的SwipeView底层原生视图默认自带渲染层,哪怕你设置了BackgroundColor="Transparent",它的裁剪范围也没和内部Frame的圆角对齐,所以会透出边角。
解决办法
方法1:给SwipeView同步圆角并开启裁剪
直接给SwipeView设置和内部Frame相同的CornerRadius,同时开启ClipToBounds="True",让SwipeView的渲染区域跟着圆角裁剪,就不会露出边角了:
<SwipeView BackgroundColor="Transparent" CornerRadius="20" ClipToBounds="True"> <SwipeView.RightItems> <SwipeItems Mode="Reveal"> <!-- SwipeItems 代码 --> </SwipeItems> </SwipeView.RightItems> <Frame Margin="20" HeightRequest="65" BorderColor="CornflowerBlue" BackgroundColor="CadetBlue" CornerRadius="20"> <!-- Frame 内部代码 --> </Frame> </SwipeView>
方法2:自定义iOS渲染器(更彻底)
如果方法1没效果,可以自定义SwipeView的iOS渲染器,手动控制原生视图的圆角和裁剪:
在iOS项目中创建如下渲染器类:
using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ExportRenderer(typeof(SwipeView), typeof(CustomSwipeViewRenderer))] namespace YourAppNamespace.iOS.Renderers { public class CustomSwipeViewRenderer : SwipeViewRenderer { protected override void OnElementChanged(ElementChangedEventArgs<SwipeView> e) { base.OnElementChanged(e); if (Control == null || Element?.Content is not Frame frame) return; // 同步Frame的圆角值 Control.Layer.CornerRadius = (nfloat)frame.CornerRadius; Control.Layer.MasksToBounds = true; Control.BackgroundColor = UIKit.UIColor.Clear; } } }
替换YourAppNamespace为你的iOS项目命名空间即可,无需额外注册,ExportRenderer特性会自动生效。
内容的提问来源于stack exchange,提问作者JBD
相关产品推荐
相关产品推荐

