简体   繁体   English

子UIView无法接收触摸事件

[英]Child UIViews unable to receive touch events

I have a UIViewController that embeds a UITableView . 我有一个嵌入UITableViewUIViewController This table view is 3/4 the size of the entire screen in height. 该表格视图的高度是整个屏幕尺寸的3/4。 the remaining 1/4th of the UIViewController has a rounded UIButton that triggers a new UIView on top of the parent view. UIViewController的其余1/4处有一个圆形的UIButton ,可在父视图的顶部触发一个新的UIView ( UITableView ). UITableView )。

Upon instantiating and calling a UIView with a background that is set to: self.backgroundColor = UIColor(black: 1, alpha: 0.5) it would normally fill the entire view with a black see-through background that will then have an additional UIView with the following constraints: 实例化并调用一个UIView ,其背景设置为: self.backgroundColor = UIColor(black: 1, alpha: 0.5) ,通常会用黑色的透明背景填充整个视图,然后再添加一个UIView以下约束:

Leading: 10
Trailing: 10
Top: 50
Bottom: 50

this, in turn, gives me a 'Card' effect on top of the tableView . 这又在tableView顶部给了我“卡片”效果。 This 'Card' view then has a UITextview property that is supposed to show the keyboard when the user taps the view with the textview embedded. 然后,此“卡”视图具有UITextview属性,该属性应在用户点击嵌入了textview的视图时显示键盘。

The Problem: Upon selecting the UITextview , or even touching this 'Card' view, the background table is being selected and interacted with. 问题:选择UITextview或什至触摸此“ Card”视图时,正在选择背景表并与之交互。 Neither the 'Card' textfield raises the keyboard nor does it make itself solely interactive as the parent table controller seems to be getting the touch events. “卡片”文本字段不会抬起键盘,也不会使其自身完全是交互式的,因为父表控制器似乎正在获取触摸事件。

Is there any solution to this problem that is encountered in iOS 11? iOS 11中遇到此问题有解决方案吗? I never experienced this issue in iOS 10. I am using iPhone X for the further note on my issue. 我从未在iOS 10中遇到此问题。我正在使用iPhone X作为我的问题的进一步说明。

Here is an example of the actual issue occurring. 这是发生实际问题的示例。

在此处输入图片说明

NOTE: If I were to select the dark area of the card itself, the table view would receive touch events but not the text area within the card view. 注意:如果我要选择卡本身的暗区域,则表视图将接收触摸事件,但不会接收卡视图中的文本区域。 The reason this card view has the keyboard showing is that I called it manually in code with the textview.becomeFirstResponder method. 卡视图显示键盘的原因是我使用textview.becomeFirstResponder方法在代码中手动调用了它。

ADDITIONAL NOTES: I have also enabled the isUserInteractionEnabled = false on the overall presenting child view. 附加说明:我还对整体显示子视图启用了isUserInteractionEnabled = false Still does not work and parent view receives touches only. 仍然不起作用,父视图仅接收触摸。

Code for the Card 卡代码

class Card: UIView {

// instantiating the textview 
var textview = UITextview()
var cardView = UIView()


private override init(frame: CGRect) {
    super.init(frame: frame)

    self.backgroundColor = UIColor(black: 1, alpha: 0.5)    
    self.cardView.backgroundColor = UIColor.white
    self.cardView.layer.cornerRadius = 8

    // Just demo the textview
    self.textview.frame = CGRect(x: 10, y: 100, width: 300. height: 100)



    self.addSubview(cardView) // adding the cardView as a subview to the background colored view
    cardView.addSubview(textview)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("Error")
}

private func ConstrainCardWith() {
    // Constraints 

}


}
 extension UIView {

func Width()-> CGFloat {
    return UIScreen.main.bounds.size.width
}
func Height()-> CGFloat{
    return UIScreen.main.bounds.size.height
}
}

问题是您的容器视图(cardview)没有框架,因此您的子视图(textview)不能触摸,因为它超出范围,因此只需在卡片视图中添加一些框架即可解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM