这就是正在发生的事情:
我在 ImageView 中添加了红色背景色,这样您就可以看到它超过了多少。
我试过 tableView(:willDisplayHeaderView:)
,我试过检查 Subviews
,我试过 AspectFit
,AspectFill
, & 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
}
}
最佳答案
首先,将 imageView
的 clipsToBounds
属性设置为 true
将防止图像溢出 imageView 的边界。
其次,image明显比imageView大,所以我觉得不是imageView溢出,是里面的image溢出。因此,您需要调整图像大小或使用 Scale*Fill 模式来显示完整图像或适当裁剪它。
HTH.
关于xcode - 表格单元格中的 UIImageView 不在框架原始框架内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391844/