iOS10 - 在UITableView标题中阻止UIButton触摸的ContentView [英] iOS10 - ContentView Blocking Touches of UIButton in UITableView Header
问题描述
我正在使用Swift 2.3和Xcode 8 Beta 1将我的应用更新到iOS10,我发现有一个 UITableViewHeaderFooterContentView
这会阻止触摸我的子类 UITableViewHeaderFooterView
上的UIButton。
I am in the process of updating my app to iOS10 with Swift 2.3 and Xcode 8 Beta 1 and I have found that there is a UITableViewHeaderFooterContentView
which is blocking touches to the UIButton on my subclass of UITableViewHeaderFooterView
.
在Xcode 8 Beta 1模拟器上,UIButton适用于iOS9.3但不适用于iOS10。
On the Xcode 8 Beta 1 simulator the UIButton works on iOS9.3 but not iOS10.
1)有没有这方面的文档?
1) Is there any documentation for this?
2)如何确保我的UI元素位于iOS10的新内容视图之上? (或允许通过 UITableHeaderFooterContentView
进行触摸)
2) How can I ensure my UI elements are on top of the new Content View in iOS10? (or allow touches through the UITableHeaderFooterContentView
)
谢谢!
表格标题
import UIKit
class TableHeader: UITableViewHeaderFooterView {
@IBOutlet weak var dayLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var addNewEventButton: UIButton!
}
代码在视图控制器中
dateCell.addNewEventButton
是在iOS10中不再接收触摸的UIButton
Code In View Controller
dateCell.addNewEventButton
is the UIButton that is no longer receiving touches in iOS10
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let tintColor = TintManager().getTintColour()
let dateCell:TableHeader = tableView.dequeueReusableHeaderFooterViewWithIdentifier("TableHeader") as! TableHeader
//dateCell.bringSubviewToFront(dateCell.addNewEventButton)
dateCell.dayLabel.text = Dates.day.uppercaseString
dateCell.dateLabel.text = Dates.date
dateCell.backgroundView = UIView(frame: dateCell.frame)
dateCell.backgroundView!.backgroundColor = tintColor
dateCell.dayLabel.textColor = UIColor.whiteColor()
dateCell.dateLabel.textColor = UIColor.whiteColor()
dateCell.addNewEventButton.backgroundColor = tintColor
dateCell.addNewEventButton.tag = section
dateCell.addNewEventButton.layer.cornerRadius = 20.0
if (savedEventView.superview === self.view) {
dateCell.addNewEventButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
dateCell.addNewEventButton.addTarget(self, action: #selector(ViewController.userPressedAddButtonToInsertSavedEvent(_:)), forControlEvents:.TouchUpInside)
} else {
dateCell.addNewEventButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
dateCell.addNewEventButton.addTarget(self, action: #selector(ViewController.userPressedAddNewEventOnTableViewHeader(_:)), forControlEvents:.TouchUpInside)
}
return dateCell
}
推荐答案
违规视图实际上是 UITableViewHeaderFooterView的
(参见 Apple Docs )。所以你应该只能使用 contentView
sendSubview(toBack :)
来阻止它干扰触摸。
The offending view is in fact the contentView
of the UITableViewHeaderFooterView
(see the Apple Docs). So you should be able just to use sendSubview(toBack:)
in order to stop it interfering with touches.
但是,如果视图是从NIB加载的,那么在iOS9下, UITableViewHeaderFooterView
似乎无法正确初始化 contentView
。虽然 contentView
属性不是可选的,但它实际上是nil,如果您尝试访问它,则会出现BAD ACCESS错误。你也不能为 contentView
设置一个值(在代码中或在IB中作为出口),因为它是一个只读属性(*)。因此,我能想到的唯一解决方案是使用 #available
来有条件地包含将contentView移动到后面的代码(如果您在iOS 10或更高版本上运行)。我会将相关代码放入您的子类中:
However, it seems that under iOS9 the UITableViewHeaderFooterView
fails to correctly initialise the contentView
if the view is loaded from a NIB. Although the contentView
property is not optional, it is in fact nil, and you get a BAD ACCESS error if you try to access it. Nor can you set a value for contentView
(either in code or as an outlet in IB) because it's a read only property (*). So the only solution I can think of is to use #available
to conditionally include code to move the contentView to the back, if you are running on iOS 10 or newer. I would put the relevant code into your subclass:
override func awakeFromNib() {
if #available(iOS 10, *) {
self.sendSubview(toBack: contentView)
}
}
(*)沉迷于狂野的猜测,我的猜测是Apple在 UITableViewCell
UITableViewHeaderFooterView 代码>。由于IB在其对象库中有 UITableViewCells
(并注意这些包括单元格的 contentView
),它可以确保单元格的contentView被正确实例化。但由于对象库中没有 UITableViewHeaderFooterView
,因此无法正确加载 contentView
。看起来他们通过实例化一个空的 contentView
在iOS10中修复它。可惜他们也没有将 UITableViewHeaderFooterView
添加到库中。
(*) Indulging in wild speculation, my guess is that Apple based the UITableViewHeaderFooterView
code heavily on UITableViewCell
. Since IB has UITableViewCells
in its object library (and notice these include the cell's contentView
), it can ensure that the cell's contentView is correctly instantiated. But since there is no UITableViewHeaderFooterView
in the object library, there's no way to get the contentView
loaded correctly. Looks like they fixed it in iOS10 by instantiating an empty contentView
. Pity they didn't also add UITableViewHeaderFooterView
to the library.
这篇关于iOS10 - 在UITableView标题中阻止UIButton触摸的ContentView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!