[英]Swift - addTarget to UIButton inside a Subview
I have a problem with my UIButton
.我的
UIButton
有问题。 Just for basic understanding:只是为了基本的理解:
popUpView
appears ( UIView
)popUpView
出现( UIView
)UIButton
which is a SubView of popUpView
-> popUpView
dismissesUIButton
这是 popUpView 的popUpView
-> popUpView
关闭That is my code for that:那是我的代码:
@objc func addWishButtonTapped(notification : Notification){
view.addSubview(popUpView)
popUpView.addSubview(popUpTextField)
popUpView.addSubview(wishButton)
// constrain popUpView
popUpView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
popUpView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100).isActive = true
popUpView.heightAnchor.constraint(equalToConstant: 200).isActive = true
popUpView.widthAnchor.constraint(equalToConstant: view.frame.width - 85).isActive = true
// constrain wishButton
wishButton.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
wishButton.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: 130).isActive = true
wishButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
wishButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
// constrain textField
popUpTextField.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
popUpTextField.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: -50).isActive = true
popUpTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
popUpTextField.widthAnchor.constraint(equalToConstant: view.frame.width - 170).isActive = true
popUpView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
popUpView.alpha = 0
UIView.animate(withDuration: 0.3) {
self.visualEffectView.alpha = 1
self.popUpView.alpha = 1
self.popUpView.transform = CGAffineTransform.identity
}
// make whishButton clickable
self.wishButton.addTarget(self, action: #selector(wishButtonTapped), for: .touchUpInside)
}
@objc func wishButtonTapped(){
print("test")
insertWhish()
dismissPopUpView()
}
Problem:问题:
whishButton
is not clickable and I have no idea why.. I'm stuck on this for a while now so I am grateful for any help, thanks:) whishButton
不可点击,我不知道为什么.. 我已经坚持了一段时间,所以我很感激任何帮助,谢谢:)
UPDATE更新
This is my View Hierarchy:这是我的视图层次结构:
Really weird, because the selected UIImage
should actually be the UIButton
which is in my case behind it (100x100 square).真的很奇怪,因为选择的
UIImage
实际上应该是UIButton
在我的情况下它后面(100x100 正方形)。 I definitely declared my whishButton
as an actual UIButton()
我肯定将我的
whishButton
声明为实际的UIButton()
first, try whit this line首先,试试这条线
self.popUpView.bringSubviewToFront(whishButton)
and if its necessary try this line如果有必要试试这条线
self.popUpView.sendSubview(yourImageBackGround)
SOLVED解决了
I solved my problem.. not in a very smooth way but it's working now.我解决了我的问题..不是很顺利,但它现在正在工作。 I just added the
button
as another subview
of my normal UIView
instead of adding it within my popUpView
.我只是将
button
添加为我的普通UIView
的另一个subview
,而不是在我的popUpView
中添加它。
I guess, you don't create any instantiate of your root class globally.我想,您不会在全球范围内创建根 class 的任何实例。
class ViewController: UIViewController {
var viewController: MyViewController
override func viewDidLoad() {
super.viewDidLoad()
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
myViewController =storyBoard.instantiateViewController(withIdentifier:"MyViewController") as! MyViewController
self.present(myViewController, animated:true, completion:nil)
myViewController.addWishButtonTapped(nil)
}
}
class MyViewController: UIViewController {
let popUpView = UIView()
let popUpTextField = UITextField()
let wishButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func addWishButtonTapped(notification : Notification?){
view.addSubview(popUpView)
popUpView.addSubview(popUpTextField)
popUpView.addSubview(wishButton)
// constrain popUpView
popUpView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
popUpView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100).isActive = true
popUpView.heightAnchor.constraint(equalToConstant: 200).isActive = true
popUpView.widthAnchor.constraint(equalToConstant: view.frame.width - 85).isActive = true
// constrain wishButton
wishButton.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
wishButton.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: 130).isActive = true
wishButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
wishButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
// constrain textField
popUpTextField.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
popUpTextField.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: -50).isActive = true
popUpTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
popUpTextField.widthAnchor.constraint(equalToConstant: view.frame.width - 170).isActive = true
popUpView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
popUpView.alpha = 0
UIView.animate(withDuration: 0.3) {
self.visualEffectView.alpha = 1
self.popUpView.alpha = 1
self.popUpView.transform = CGAffineTransform.identity
}
// make whishButton clickable
self.wishButton.addTarget(self, action: #selector(wishButtonTapped), for: .touchUpInside)
}
@objc func wishButtonTapped(){
print("test")
insertWhish()
dismissPopUpView()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.