更改 UITableViewCell 中 imageView 的位置
Change position of imageView in UITableViewCell
我想更改默认 imageView 在 UITableViewCell 中的位置 (cell.imageView...)。是否可以这样做,还是我必须创建一个新的?
您可以通过以下方式添加新的图像视图:
let iconImageView = UIImageView()
iconImageView.frame = CGRectMake(10, 10, 50, 50)
iconImageView.image = UIImage(named: "image_name")
// you have to add the imageview to the content view of the cell
contentView.addSubview(iconImageView)
Swift 4:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let iconImageView = UIImageView()
iconImageView.image = UIImage(named: "yourImageName")
cell.addSubview(iconImageView)
iconImageView.translatesAutoresizingMaskIntoConstraints = false
iconImageView.topAnchor.constraint(equalTo: cell.topAnchor, constant: 20).isActive = true
iconImageView.rightAnchor.constraint(equalTo: cell.rightAnchor, constant: -40).isActive = true
iconImageView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
iconImageView.widthAnchor.constraint(equalTo: cell.heightAnchor, constant: -20).isActive = true
return cell
}
我想更改默认 imageView 在 UITableViewCell 中的位置 (cell.imageView...)。是否可以这样做,还是我必须创建一个新的?
您可以通过以下方式添加新的图像视图:
let iconImageView = UIImageView()
iconImageView.frame = CGRectMake(10, 10, 50, 50)
iconImageView.image = UIImage(named: "image_name")
// you have to add the imageview to the content view of the cell
contentView.addSubview(iconImageView)
Swift 4:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let iconImageView = UIImageView()
iconImageView.image = UIImage(named: "yourImageName")
cell.addSubview(iconImageView)
iconImageView.translatesAutoresizingMaskIntoConstraints = false
iconImageView.topAnchor.constraint(equalTo: cell.topAnchor, constant: 20).isActive = true
iconImageView.rightAnchor.constraint(equalTo: cell.rightAnchor, constant: -40).isActive = true
iconImageView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
iconImageView.widthAnchor.constraint(equalTo: cell.heightAnchor, constant: -20).isActive = true
return cell
}