UITableViewCell, вложенный в swift 3, всегда имеет contentView 320 точек - PullRequest
0 голосов
/ 10 мая 2018

Я пытаюсь создать UILabel в подклассе UITableViewCell, чтобы сделать его полной шириной ячейки. Но contentView всегда 320pt, если в iPhone 7 или 7+.

Код:

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 ночи. Спасибо.

Ответы [ 3 ]

0 голосов
/ 10 мая 2018

Вы можете попробовать авто-макет

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

}
0 голосов
/ 11 мая 2018

Может быть, вы забыли установить ограничения для вашего UITableView.Попробуйте установить ограничение ширины табличного представления равным ширине суперпредставления.

В ViewController:

self.view.addConstraint(NSLayoutConstraint(item: self.tableView,  attribute: .width,  relatedBy: .equal,  toItem: self.view,  attribute: .width,  multiplier: 1.0,  constant: 0))
0 голосов
/ 10 мая 2018

Вы должны добавить свой ярлык внутри override init(style: UITableViewCellStyle, reuseIdentifier: String!), но чтобы прочитать правильный кадр, у вас есть переопределение override func layoutSubviews() и прочитать правильный кадр

...