【问题标题】:UIImageView in table cell is not staying within frame original frame?表格单元格中的 UIImageView 不在框架原始框架内?
【发布时间】:2016-02-14 12:37:54
【问题描述】:

所以这就是正在发生的事情:

我在图像视图中添加了红色背景色,这样您就可以看到它已经过去了多少。

我试过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
}

}

【问题讨论】:

    标签: xcode swift uitableview uiimageview nslayoutconstraint


    【解决方案1】:

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

    其次,图像明显比imageView大,所以我认为imageView没有溢出,里面的图像溢出了。因此,您需要调整图像大小或使用 Scale*Fill 模式显示完整图像或根据需要进行裁剪。

    HTH。

    【讨论】:

      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      相关资源
      最近更新 更多