我有一个UITableCell,它有一个ImageView。我试图给ImageView圆角。我尝试过以下方法:
cell.ImageView.ClipsToBounds = true;
cell.ImageView.Layer.BorderWidth = 1;
cell.ImageView.Layer.MasksToBounds = true;
cell.ImageView.Layer.CornerRadius = 20;
但这没有任何效果。任何人都可以解释或演示代码示例如何实现这种效果?
答案 0 :(得分:1)
cell.ImageView.Layer.BorderWidth = 1;
cell.ImageView.Layer.CornerRadius = 20;
cell.ImageView.Layer.MasksToBounds = true;
尝试在最后掩饰边界。
答案 1 :(得分:1)
答案 2 :(得分:0)
首先创建列表视图
namespace MaxenceTest.Renderers
{
public class MyViewCell : ViewCell
{
public static BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(double), typeof(MyViewCell),default(double));
public double CornerRadius
{
get { return (double)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public static BindableProperty BackgroundColorProperty = BindableProperty.Create(nameof(BackgroundColor), typeof(Color), typeof(MyViewCell), default(Color));
public Color BackgroundColor
{
get { return (Color)GetValue(BackgroundColorProperty); }
set { SetValue(BackgroundColorProperty, value); }
}
}
}
在您的Xamarin iOS项目中实现它
[assembly: ExportRenderer(typeof(MyViewCell), typeof(MyViewCellIOS))\]
namespace MaxenceTest.iOS.Renderers
{
public class MyViewCellIOS : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
UITableViewCell viewCell = base.GetCell(item, reusableCell, tv);
if(viewCell != null)
{
if(item is MyViewCell mycell)
{
UIView custom = new UIView();
viewCell.ContentView.Layer.BackgroundColor = mycell.BackgroundColor.ToCGColor();
viewCell.ContentView.Layer.CornerRadius = new nfloat(mycell.CornerRadius);
}
}
return viewCell;
}
}
}
在xcml中使用
<StackLayout BackgroundColor="Transparent">
<ListView
x:Name="list"
BackgroundColor="Transparent"
SeparatorColor="Transparent"
SeparatorVisibility="None"
HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<myControls:MyViewCell CornerRadius="20" BackgroundColor="Fuchsia">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout Margin="10">
<Label FontSize="20" TextColor="White" FontAttributes="Bold" Text="{Binding title}"/>
<Label FontSize="15" TextColor="White" Text="{Binding resume}"/>
</StackLayout>
</Grid>
</myControls:MyViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
享受