2017-12-18 158 views
0

我有一个UITableViewController与静态表格单元格。使UIImage clipsToBound通过UITableViewCell

而我试图用这个单元格UIImageView和这个图像clipToBounds,但它不工作。

这是我想要做的一个示例。

enter image description here

但是当我做一个UITableView并设置UIImage的clipToBoundsTRUE

结果是:

enter image description here

我只是需要一种方法来使UIImageView喜欢第一个图像无论如何与UITableView单元格?

,这是我的UIImageView约束:

enter image description here

,这里是我的代码:

class ProfileTableViewController: UITableViewController { 

    @IBOutlet weak var userImage: UIImageView! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 



     // image view 

     self.userImage.layer.cornerRadius = self.userImage.frame.width/2 
     self.userImage.clipsToBounds = true 




    } 

// MARK: - 表视图的数据源

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 3 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    var number = 0 

    if section == 0 
    { 
     number = 1 
    }else if section == 1 
    { 
     number = 4 
    }else if section == 2 
    { 
     number = 1 
    } 

    return number 
} 


override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 100 
} 

} 
+0

据我可以看到图像被裁剪好(如果白色背景代表一个单元格和视图的灰色其余部分)。你应该检查约束和UIImageView定位。 – Adamsor

+0

你能告诉我你输出图像中的哪一部分是tableViewCell。我的意思是全图像或白色部分 –

+0

@Adamsor我更新了我的问题检查显示图像约束的最后一个图像 – Muhammed

回答

2

我找到了解决办法,当我加入此方法,并添加自定义区段标题

,并给它clipToBounds =假

问题解决了

我刚刚加入:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let view = UIView() 
     view.clipsToBounds = false 

     return view 
    } 

,它的工作完美:))

感谢所有试图帮助我

1

为什么clipsToBounds = true ? 如果图像必须outside细胞,那么我想你想的正好相反:

yourView.clipsToBounds = false 
+0

我做到了,但仍然没有工作 – Muhammed

1

刚刚创建图像视图插座让拐角半径itswidth/2(确保高度和宽度相同的大小),并夹界定为真。例如:

imageView = imageView.frame.size.width/2; 
imageView.clipsToBounds = true; 
+0

仍然无法正常工作 – Muhammed

+0

显示我们的代码的东西 –

+0

检查最后编辑 – Muhammed

0

这可能会解决您的问题。在indexPath

细胞用于行
let height = CGFloat(cell.frame.size.height-10) 
let leading = (cell.frame.size.width - height)/2 
let top = CGFloat(18) 

let myImage = UIImageView(frame: CGRect(x: leading, y: top, width: height, height:height)) 
myImage.layer.masksToBounds = true 
myImage.layer.cornerRadius = (height)/2 
cell.contentView.addSubview(myImage) 

// OR

cell.myImage.layer.cornerRadius = cell.myImage.frame.width/2 
cell.myImage.clipsToBounds = true