通过UITableViewCell创建UIImage clipsToBound

问题描述 投票:-1回答:5

我有一个静态表格单元格的UITableViewController

而我正试图用UIImageView制作一个单元格和这个图像clipToBounds到这个单元格,但它不起作用。

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

enter image description here

但是当我制作一个UITableView并将UIImage clipToBounds设置为TRUE时

结果是:

enter image description here

我只是需要一种方法来使UIImageView像第一张图像一样用UITableView Cell做到这一点?

这是我的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 
}

}
ios swift
5个回答
1
投票

只需为图像视图创建出口使其角半径为width / 2(确保高度和宽度相同)并使剪辑边界为true。对于前:

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

1
投票

当我添加此方法并添加自定义Section标头时,我找到了解决方案

并给它clipToBounds = false

问题解决了

我刚补充说:

  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
投票

这可能会解决您的问题。在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

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.