ios - 使用自定义单元格时,旋转会导致表格 View 超出安全区域

标签 ios swift xcode uitableview custom-cell

旋转具有自定义单元格的 TableView Controller 时遇到问题。我创建了一个简单的 TableView Controller 并将其设置如下

import UIKit

class MyTableTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.register(CustomCellMain.self, forCellReuseIdentifier: "customCellMain")
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCellMain") as! CustomCellMain
        cell.mainImage = UIImage(named: "testtest.jpeg")
        cell.message = "Hello"
        cell.layoutSubviews()
        return cell
    }
}

我的自定义单元格如下所示:

import UIKit

class CustomCellMain: UITableViewCell {
    var message: String?
    var mainImage: UIImage?

    var messageView: UILabel = {
        var label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        //labelView.isScrollEnabled = false
        label.textAlignment = .right
        label.font = .systemFont(ofSize: 27)
        return label
    }()

    var mainImageView: UIImageView = {
        var imageView = UIImageView()
        imageView.sizeToFit()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = 0
        return imageView
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addSubview(mainImageView)
        self.addSubview(messageView)

        //Image
        mainImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10).isActive = true
        mainImageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 9).isActive = true
        mainImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -8).isActive = true
        mainImageView.widthAnchor.constraint(equalToConstant: 75).isActive = true
        mainImageView.heightAnchor.constraint(equalToConstant: 75).isActive = true

        //Text
        messageView.leftAnchor.constraint(equalTo: self.mainImageView.rightAnchor).isActive = true
        messageView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15).isActive = true
        messageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        messageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 1).isActive = true
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        if let message = message {
            messageView.text = message
        }
        if let image = mainImage {
            mainImageView.image = image
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

垂直看起来不错:

enter image description here

但是当我旋转它时,它会覆盖左侧或右侧

enter image description here

enter image description here

我不知道是什么原因造成的。任何帮助将不胜感激!!

最佳答案

您可以使用Main.Storyboard

来解决它
  • 转到Main.Storyboard
  • 选择您的UITableView
  • 转到它的尺寸检查器
  • 在该复选标记中选择内容 View [TickMark] 插入到安全区域

如果这不起作用,请尝试内容插入下拉菜单选项

  • 有时从不自动选项在其中起作用。

  • 在自定义单元格文件中尝试 imageView.translatesAutoresizingMaskIntoConstraints = true

  • 在 Main.Storyborad 中,选择您的单元格并在 SizeInspector 的 Layoutdropdown 中指定将蒙版转换为约束

关于ios - 使用自定义单元格时,旋转会导致表格 View 超出安全区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59332819/

相关文章:

iphone - 链接器命令失败 iOS

SwiftUI - 如何访问列表中的数据

iphone - block 键盘输入

ios - 在 Xcode 中重命名文件和项目的指南

core-data - 将 NSSet 转换为 Swift 数组

ios - 从 Swift 中的模拟调用 super

ios - UINavigationController奇怪的流行动画

ios - 点击时更改 UICollectionViewCell 上的 ImageView 图像

ios - 为什么我对两个日期的比较不起作用?

ios - 将框架添加到现有的 Cocoapods 设置