调整UITableViewCell类内的UILabels大小(即宽度和高度)?

时间:2019-07-08 22:07:14

标签: swift xcode uitableview uilabel

我创建了一个自定义UITableViewCell类,如下所示,由此我定义了要显示在自定义单元格中的项目(特别是UILabel项目):

import UIKit

import ChameleonFramework

class IsectionsCustomTableViewCell: UITableViewCell {

    let sectionDesignationLabelTopPadding: CGFloat = 10

    let sectionDesignationLabelLeftPadding: CGFloat = 10

    let sectionDesignationLabelRightPadding: CGFloat = 10

    let depthOfSectionLabelTopPadding: CGFloat = 0

    let depthOfSectionLabelLeftPadding: CGFloat = 10

    let depthOfSectionLabelRightPadding: CGFloat = 10

    let webThicknessLabelTopPadding: CGFloat = 10

    let webThicknessLabelLeftPadding: CGFloat = 10

    let webThicknessLabelRightPadding: CGFloat = 10

    var sectionDesignationLabel: UILabel = {

        let label = UILabel()

        label.backgroundColor = UIColor.yellow

        label.textAlignment = .left

        label.translatesAutoresizingMaskIntoConstraints = false

        label.textColor = UIColor(hexString: "#F27E63")

        return label

    }()

    var depthOfSectionLabel: UILabel = {

        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))

        label.backgroundColor = UIColor.blue

        label.translatesAutoresizingMaskIntoConstraints = false

        label.textColor = UIColor(hexString: "#F27E63")

        return label

    }()

        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        addSubview(sectionDesignationLabel)

        addSubview(depthOfSectionLabel)

        applyAppropriateSizeAndConstraintsForCellItems()

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    func applyAppropriateSizeAndConstraintsForCellItems() {

        sectionDesignationLabel.frame.size.width = 200

        NSLayoutConstraint.activate([

            sectionDesignationLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: sectionDesignationLabelLeftPadding),

            sectionDesignationLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -1*sectionDesignationLabelRightPadding),

            sectionDesignationLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: sectionDesignationLabelTopPadding),

            depthOfSectionLabel.topAnchor.constraint(equalTo: sectionDesignationLabel.bottomAnchor, constant: depthOfSectionLabelTopPadding),

            depthOfSectionLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: depthOfSectionLabelLeftPadding),

            depthOfSectionLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -1 * (((self.frame.size.width)/2) + depthOfSectionLabelRightPadding)),

            ])

    }

}

从depthOfSectionLabel的声明中可以看出,我在其中声明了其框架的宽度和高度。但是,显示我的表格时,这些值不会反映出来(请参阅附件图像)。显示的宽度远远大于我为此特定标签指定的宽度50?

我的问题是如何调整要在tableView自定义单元格中显示的各种标签的宽度?

enter image description here

0 个答案:

没有答案