我有一个静态表格单元格的UITableViewController
。
而我正试图用UIImageView
制作一个单元格和这个图像clipToBounds
到这个单元格,但它不起作用。
这是我想要做的一个例子。
但是当我制作一个UITableView
并将UIImage clipToBounds设置为TRUE时
结果是:
我只是需要一种方法来使UIImageView
像第一张图像一样用UITableView
Cell做到这一点?
这是我的UIImageView约束:
这是我的代码:
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
}
}
只需为图像视图创建出口使其角半径为width / 2(确保高度和宽度相同)并使剪辑边界为true。对于前:
imageView = imageView.frame.size.width/2;
imageView.clipsToBounds = true;
当我添加此方法并添加自定义Section标头时,我找到了解决方案
并给它clipToBounds = false
问题解决了
我刚补充说:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.clipsToBounds = false
return view
}
它完美地工作:))
谢谢所有人试图帮助我
为什么clipsToBounds = true
?如果图像必须是outside
细胞,那么我想你想要相反:
yourView.clipsToBounds = false
这可能会解决您的问题。在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)
//要么
cell.myImage.layer.cornerRadius = cell.myImage.frame.width / 2
cell.myImage.clipsToBounds = true