ios - 在 swift 3 中子类化的 UITableViewCell 始终具有 320 点的 contentView

标签 ios iphone swift uitableview swift3

我正在尝试在我的子类 UITableViewCell 中创建一个 UILabel 以使其成为单元格的整个宽度。但如果在 iPhone 7 或 7+ 中,contentView 始终为 320pt。

代码是:

import UIKit
import SwipeCellKit

class ReuseableCell: SwipeTableViewCell
{
    public var test = UILabel();

    var animator: Any?

    override init(style: UITableViewCellStyle, reuseIdentifier: String!)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier);

        // Always 320pt and I want full width of the table cell
        var width = self.contentView.frame.width;

        test = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: 100));


        test.text = "Lorem ipsum...";

        ...

我已经坚持了 2 个晚上。谢谢。

最佳答案

你可以试试自动布局

override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
    super.init(style: style, reuseIdentifier: reuseIdentifier);


    test = UILabel(frame: CGRect.zero);

    self.contentView.addSubview(test)

    test.translatesAutoresizingMaskIntoConstraints = false

    test.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true

    test.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true

    test.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true

    test.heightAnchor.constraint(equalToConstant: 200).isActive = true

    test.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true

}

关于ios - 在 swift 3 中子类化的 UITableViewCell 始终具有 320 点的 contentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50267580/

相关文章:

ios - 转换到 MainVC 会给出没有 subview 的空 View

ios - 如果用户在钥匙串(keychain)中登录,则 sender.tag 不设置为 1?

ios - 如何在 iOS 中将解析读取图像与屏幕显示同步

objective-c - 避免嵌套 block

iphone - 当应用程序进入后台或手机被锁定时如何获取位置?

swift - 返回上一个 View Controller 时的 UIImage 问题

ios - 我无法使用 swift 3.0 编辑数据并将其保存到核心数据?

iOS SKMaps 不加载注释(Xcode 6,Objective C)

iphone - iPhone 3G 和 iPhone4 不工作时 iPad 崩溃

ios - 在 Controller 中搜索单词