【问题标题】:Can't get corner radius to work无法使拐角半径起作用
【发布时间】:2015-03-09 15:49:34
【问题描述】:

您好,我正在编写一个聊天应用程序,但我不希望个人资料图片具有拐角半径。这是我的代码。顺便说一句,我使用 Parse 作为后端。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    var imageView = PFImageView()
    let user = self.users[indexPath.row]
    imageView.layer.cornerRadius = imageView.frame.size.width / 2
    imageView.layer.masksToBounds = true
    imageView.file = user[PF_USER_PICTURE] as? PFFile
    cell.textLabel?.text = user[PF_USER_FULLNAME] as? String
    cell.textLabel?.font = UIFont.boldSystemFontOfSize(24)
    cell.imageView?.image = imageView.image



    return cell
}

更新: 问题是我的 cell.imageview 没有框架。 感谢 Max K 的回答!

【问题讨论】:

    标签: ios swift rounded-corners cornerradius


    【解决方案1】:
    var imageView = PFImageView()
    

    是不同的图像视图:

    cell.imageView?.image = imageView.image
    

    屏幕上显示的是您的单元格的图像视图,而不是PFImageView 的这个实例。如果您想将PFImageView 作为您的单元格图像视图的一个类,您需要在情节提要中或通过子类化您的单元格来设置它。

    【讨论】:

      【解决方案2】:

      您将cornerRadius应用于此处创建的imageView层:

      var imageView = PFImageView()

      但是你只使用它的 image 属性并将其分配给单元格的 imageView:

      cell.imageView?.image = imageView.image

      cornerRadius 应用于单元格的imageView

      【讨论】:

      • 当我尝试我得到这个错误:'UIImageView' 没有名为'cornerRadius' 的成员 -
      • 应用到层,但是cell的imageView层,不是你创建的那个。
      • 我尝试使用 cell.imageView?.layer.cornerRadius = imageView.frame.size.width / 2 并没有给我错误,但图像仍然是正方形
      • @JohanEnstam 在您的原始代码中,imageView (PFImageView) 没有建立框架,因为您也没有将其添加到屏幕上,也没有明确设置框架
      • 好的,如何建立框架?
      【解决方案3】:

      您正在将cornerRadius 设置在以后不会添加到屏幕的imageView 上。

      你必须在cell.imageView上设置它

      if let imageView = cell.imageView {
          imageView.layer.cornerRadius = imageView.frame.size.width / 2
      }
      

      【讨论】:

      • 当我尝试我得到这个错误:'UIImageView'没有名为'cornerRadius'的成员
      • @JohanEnstam,我的错,当然应该是imageView.layer.cornerRadius。查看更新后的帖子。
      【解决方案4】:

      您正在向imageView 中添加角

      var imageView = PFImageView()

      改为将角添加到cell.imageView。似乎您正在使用 PFImageView 从文件中下载图像。所以你可以像这样修改代码

      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      
      let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
      
      var imageView = PFImageView()
      let user = self.users[indexPath.row]
      cell.imageView.layer.cornerRadius = imageView.frame.size.width / 2
      cell.imageView.layer.masksToBounds = true
      imageView.file = user[PF_USER_PICTURE] as? PFFile
      cell.textLabel?.text = user[PF_USER_FULLNAME] as? String
      cell.textLabel?.font = UIFont.boldSystemFontOfSize(24)
      cell.imageView?.image = imageView.image
      
      return cell
      }
      

      否则将cell.imageView的imageView设为PFImageView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-12
        • 1970-01-01
        相关资源
        最近更新 更多