[英]iOS Button not work in custom view
There are two custom views - viewA and viewB.ViewB is a small view added in viewA. 有两个自定义视图-viewA和viewB.ViewB是在viewA中添加的一个小视图。 At first viewA is filled in the whole screen.
首先,在整个屏幕中填充视图A。 ViewB is located in the bottom of viewA(out of the screen).
ViewB位于viewA的底部(屏幕外)。 When click a button in viewA, viewB bottom constant of constraints will be -100, thus viewB will display in the bottom of the screen.
当单击viewA中的按钮时,viewB约束的底部常量将为-100,因此viewB将显示在屏幕底部。 But there is a button in viewB did not response its selector.
但是viewB中有一个按钮没有响应其选择器。 Here is my code:
这是我的代码:
In ViewA 在ViewA中
let viewB: ViewB = {
let view = ViewB(
view.setup()
return view
}()
viewB constraints: viewB约束:
viewB.snp.makeConstraints { (make) in
make.trailing.leading.equalTo(self)
make.height.equalTo(100)
make.top.equalTo(self.snp.bottom)
}
when click button to arouse viewB 当单击按钮引起视图B时
UIView.animate(withDuration: 0.5) {
self.snp.updateConstraints({ (make) in
make.bottom.equalTo(-100)
})
}
self.layoutIfNeeded()
In viewB: 在viewB中:
class ViewB: UIView {
let b: UIButton = {
let button = UIButton(type: UIButtonType.custom)
button.addTarget(self, action: #selector(btnClicked), for: UIControlEvents.touchUpInside)
button.backgroundColor = UIColor.green
button.setTitle("Button", for: .normal)
return button
}()
func btnClicked() {
print("btnClicked")
}
func setup() {
addSubview(b)
b.snp.makeConstraints { (make) in
make.leading.top.bottom.equalTo(self)
make.width.equalTo(100)
}
}
}
Your code by itself works fine. 您的代码本身可以正常工作。 So the issue is probably something like the button being overlaid by another view which takes the touch actions.
因此,问题可能出在类似按钮被另一个执行触摸操作的视图所覆盖的事情上。 You can test this using the view debugger or provide a link to your project so one of us can take a look to see what is going on.
您可以使用视图调试器对此进行测试,也可以提供指向您项目的链接,以便我们中的一个可以看看发生了什么。
i think , your view is going out of its super View or Any other view is coming on its upper layer. 我认为,您的视图将超出其超级视图,或者任何其他视图将进入其上层。 please see by this
请看这里
superview.clipsToBounds : YES
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.