xcode - 表格单元格中的 UIImageView 不在框架原始框架内?

标签 xcode swift uitableview uiimageview nslayoutconstraint

这就是正在发生的事情:

enter image description here

我在 ImageView 中添加了红色背景色,这样您就可以看到它超过了多少。

我试过 tableView(:willDisplayHeaderView:),我试过检查 Subviews,我试过 AspectFitAspectFill, & ScaletoFill 但 UIImageView 不断超出其原始边界,我不明白为什么。

这是我的 UITableViewController 中的一些代码:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("scheduleCell", forIndexPath: indexPath) as! ScheduleTableViewCell
    let eventList = schedule?.schedule[dayOfWeek[indexPath.section]]!
    let event = eventList![indexPath.row]
    cell.labelEvent.text = event.act.rawValue
    cell.labelTime.text = "\(event.getTime(event.start)) - \(event.getTime(event.end))"

    return cell
}

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let tableViewCell = cell as! ScheduleTableViewCell
    let eventList = schedule?.schedule[dayOfWeek[indexPath.section]]!
    let event = eventList![indexPath.row]
    tableViewCell.imageView?.image = UIImage(named: "\(schedule!.potato.mode)-\(event.act.rawValue)")
    tableViewCell.imageView?.backgroundColor = UIColor.redColor()
    tableViewCell.imageView?.contentMode = .ScaleAspectFit

}

这是我的自定义单元格类:

class ScheduleTableViewCell: UITableViewCell {

@IBOutlet weak var imageEvent: UIImageView!
@IBOutlet weak var labelEvent: UILabel!
@IBOutlet weak var labelTime: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

enter image description here

最佳答案

首先,将 imageViewclipsToBounds 属性设置为 true 将防止图像溢出 imageView 的边界。

其次,image明显比imageView大,所以我觉得不是imageView溢出,是里面的image溢出。因此,您需要调整图像大小或使用 Scale*Fill 模式来显示完整图像或适当裁剪它。

HTH.

关于xcode - 表格单元格中的 UIImageView 不在框架原始框架内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391844/

相关文章:

ios - 在设备控制台中查看 os_log 消息

swift - 可选类型 CGFloat 的值未在 Swift 中展开错误

ios - 具有多个 subview 问题的容器 View

ios - 可以通过滑动删除表格行,但不能在编辑模式下

ios - 带有标题的 TableView 在触底之前反弹

ios - 在 UISearchController 中搜索时无法在 tableView 中滚动

ios - 更改 "Created by"_username_

ios - 使用 Swift 离开 View Controller 后自动关闭

objective-c - 在 swift 中创建具有自定义 View 的 menulet osx 应用程序

快速获取tableview单元格的所有值