简体   繁体   English

以编程方式更改UIImage位置

[英]Changing the UIImage position programmarically

I have a UITableView with sections. 我有一个带有部分的UITableView。 I added a UImage in each cell, but it is on the left side, how can I change it to make it go to the right side? 我在每个单元格中添加了一个UImage,但是它在左侧,如何更改它使其移到右侧?

this is my code for each cell: 这是我每个单元格的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cells")
    cell?.textLabel?.text = data[indexPath.section].subType[indexPath.row]
    cell?.textLabel?.textAlignment = .right
    cell?.textLabel?.font = UIFont(name: "GESSTwoLight-Light", size: 15)
    cell!.isUserInteractionEnabled = false;
    cell!.imageView?.image = UIImage(named: "username.png")

    return cell!
}

This is my UITableView 这是我的UITableView

you simply have to do the following in the tableView(_:cellForRowAt:) method: 您只需在tableView(_:cellForRowAt:)方法中执行以下操作:

cell.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
cell.textLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
cell.imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)

This will create the following 这将创建以下内容

Credits goes to liau-jian-jie for sharing this solution , which also can be applied to UIButtons. 致谢liau-jian-jie 共享此解决方案该解决方案也可以应用于UIButtons。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM