I want to make a custom view, that will contain the following:
This is my code which is inside a custom view:
func setup() {
// add green view
let greenView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
greenView.backgroundColor = UIColor.greenColor()
greenView.userInteractionEnabled = true
addSubview(greenView)
// add first image
let image1 = UIImageView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
image1.image = UIImage(named: "checked2")
image1.userInteractionEnabled = true
greenView.addSubview(image1)
// add white image
let whiteView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
whiteView.backgroundColor = UIColor.yellowColor()
whiteView.userInteractionEnabled = true
greenView.addSubview(whiteView)
// add second image
let image2 = UIImageView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
image2.image = UIImage(named: "checked")
image2.userInteractionEnabled = true
whiteView.addSubview(image2)
// add button
let button = UIButton(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: Selector("animation:"), forControlEvents: UIControlEvents.TouchUpInside)
button.userInteractionEnabled = true
addSubview(button)
}
func animation(button: UIButton) {
print("tapped")
}
The button, which is red appears on top, but the action is not called when clicking on it.
I tried to set userInteractionEnabled
on all of the elements, but with no effect.
您发布的代码运行正常,问题出在包含所有视图的容器视图中,也许userInteractionEnabled
对于容器视图为false。
默认情况下,按钮用户交互是打开的,无需显式设置。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.