【问题标题】:Xamarin.iOS Rounded corners on UITableViewCell ImageViewXamarin.iOS UITableViewCell ImageView 上的圆角
【发布时间】:2016-08-11 14:59:20
【问题描述】:

我有一个 UITableCell,它有一个 ImageView。我试图给那个 ImageView 圆角。我尝试了以下方法:

    cell.ImageView.ClipsToBounds = true;
    cell.ImageView.Layer.BorderWidth = 1;
    cell.ImageView.Layer.MasksToBounds = true;
    cell.ImageView.Layer.CornerRadius = 20;

但这没有任何效果。谁能用代码示例解释或演示如何实现这种效果?

【问题讨论】:

    标签: ios uitableview uiimageview xamarin.ios


    【解决方案1】:

    还有一个问题啊,不过应该和你一样正常,有截图,直接指出问题所在,我可以帮你。

    或者只是发布您所做的更详细的代码,然后我也许可以找到问题。

    【讨论】:

    • 啊我认为问题是因为我扩展了 UITableViewCell,我已将圆角代码添加到自定义 UITableViewCell 的构造函数中,现在它可以工作了!
    【解决方案2】:
    cell.ImageView.Layer.BorderWidth = 1;
    cell.ImageView.Layer.CornerRadius = 20; 
    cell.ImageView.Layer.MasksToBounds = true;
    

    尝试在最后屏蔽边界。

    【讨论】:

    • 嗨,Evgeny,这没有任何效果。图像仍然呈现没有圆角。
    • 请尝试不使用cell.ImageView.ClipsToBounds = true; 代码行...只需将其注释并重试。
    【解决方案3】:

    ViewCellRenderer

    1. 首先创建你的列表视图

      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);  }  
              }  
          }  
      }
      
    2. 在您的 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;  
              }  
          }  
      }
      
    3. 在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>
      
    4. 享受

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      相关资源
      最近更新 更多