【问题标题】:iOS Unable to change X position of label in UITableViewCelliOS 无法更改 UITableViewCell 中标签的 X 位置
【发布时间】:2021-02-05 19:57:49
【问题描述】:

在左侧的标签下方和右侧的开关以编程方式出现在 TableViewCell 中。我可以使用以下方法控制开关的 X 位置:

switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true

但左侧标签的可比较指令什么也不做,它始终保持在默认的左侧位置。

 labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100).isActive = true

我是否必须声明标签的宽度,而使用开关它是已知的?

    lazy var labelControl: UILabel = {
        let labelControl = UILabel()
        labelControl.translatesAutoresizingMaskIntoConstraints = false
        return labelControl
    }()
    
    lazy var switchControl: UISwitch = {
        let switchControl = UISwitch()
        switchControl.isOn = true
        switchControl.onTintColor = UIColor.orange
        switchControl.translatesAutoresizingMaskIntoConstraints = false
        switchControl.addTarget(self, action: #selector(handleSwitchAction), for: .valueChanged)
        return switchControl
    }()
    
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview(labelControl)
        labelControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100).isActive = true
        addSubview(switchControl)
        switchControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
    }

【问题讨论】:

    标签: ios uitableview tableview


    【解决方案1】:

    您的代码正在尝试添加多个相同的锚约束。据推测,您会在调试控制台中收到一个自动布局警告来解释这一点。

    您需要创建一个可以修改的约束:

    lazy var labelControl: UILabel = {
        let labelControl = UILabel()
        labelControl.translatesAutoresizingMaskIntoConstraints = false
        return labelControl
    }()
    
    lazy var switchControl: UISwitch = {
        let switchControl = UISwitch()
        switchControl.isOn = true
        switchControl.onTintColor = UIColor.orange
        switchControl.translatesAutoresizingMaskIntoConstraints = false
        switchControl.addTarget(self, action: #selector(handleSwitchAction), for: .valueChanged)
        return switchControl
    }()
    
    // add these vars
    var labelControlLeftAnchor: NSLayoutConstraint!
    var switchControlRightAnchor: NSLayoutConstraint!
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview(labelControl)
        labelControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        //labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100).isActive = true
    
        // create labelControlLeftAnchor
        labelControlLeftAnchor = labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100)
        labelControlLeftAnchor.isActive = true
        
        addSubview(switchControl)
        switchControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        //switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
    
        // create switchControlRightAnchor
        switchControlRightAnchor = switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20)
        switchControlRightAnchor.isActive = true
        
    }
    

    现在,您可以更改 .constant 值以“移动”对象:

    // for example
    labelControlLeftAnchor.constant = 40
    switchControlRightAnchor.constant = -40
    

    【讨论】:

    • 唐 - 我确信这是正确的方法 - 非常感谢。我仍然无法移动左边的标签:右边的标签完美无缺。早上起来看看。
    • @EdwardHasted - 您是否收到自动布局警告/错误消息?作为旁注 - 您应该将子视图添加并限制它们到单元格的 contentView ...而不是单元格本身。
    • 正在编写教程。文本显示为:textlabel?.text = sectionType?.description。当我将其更改为 labelControl.text = sectionType.description 时,约束起作用了。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 2014-11-19
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    相关资源
    最近更新 更多