【发布时间】:2016-02-04 04:42:41
【问题描述】:
我以编程方式创建UIImageView,并在其上添加一个UIButton。但按钮不可点击。
当我点击按钮My account 时,它会像第二张照片一样显示。 UIImageView 将出现,并且上面有一个按钮 My profile。但是My profile 在我单击时不起作用。当我再次单击My account 时,UIImageView 会按我的预期消失(MY profile 也会消失)
将action: "clickProfile"修改为action: "clickProfile:",还是不行。当我点击按钮myAccount 时,UIImageView 将出现,当我点击UIImageView 上的按钮时,它应该打印“工作”。当我单击myAccount 时,UIImageView 将消失。
这是我所有的代码:
class userViewController: UIViewController,UIPopoverPresentationControllerDelegate {
let picker = UIImageView(image: UIImage(named: "picker"))
func createPicker()
{
picker.frame = CGRect(x: ((self.view.frame.width / 2) - 143), y: 200, width: 286, height: 291)
picker.alpha = 0
picker.hidden = true
picker.userInteractionEnabled = true
self.view.addSubview(picker)
}
func clickProfile(sender:UIButton!)
{
print("should work")
}
override func viewDidLoad() {
super.viewDidLoad()
self.picker.userInteractionEnabled = true
createPicker()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func myAccount(sender: UIButton) {
picker.hidden ? openPicker() : closePicker()
}
func openPicker()
{
self.picker.hidden = false
UIView.animateWithDuration(0.3,
animations: {
self.picker.frame = CGRect(x: ((self.view.frame.width / 2) - 143), y: 230, width: 286, height: 291)
self.picker.alpha = 1
})
var offset = -180
let buttonProfile:UIButton = UIButton(type: UIButtonType.Custom) as UIButton
buttonProfile.frame = CGRect(x: 140, y: offset, width: 260, height: 43)
buttonProfile.setTitleColor(UIColor(red: 0.85, green: 0.22, blue: 0.71, alpha: 1.0), forState: .Normal)
// button.setTitleColor(UIColor(rgba: feeling["color"]!), forState: .Normal)
buttonProfile.setTitle("My profile", forState: .Normal)
buttonProfile.tag = 0
buttonProfile.addTarget(self, action: "clickProfile:", forControlEvents: UIControlEvents.TouchUpInside)
picker.addSubview(buttonProfile)
}
func closePicker()
{
UIView.animateWithDuration(0.3,
animations: {
self.picker.frame = CGRect(x: ((self.view.frame.width / 2) - 143), y: 200, width: 286, height: 291)
self.picker.alpha = 0
},
completion: { finished in
self.picker.hidden = true
}
)
}
}
我已经启用 userInteractionEnabled 为真,但 UIImageView 上的按钮仍然不起作用。它是怎么发生的?
【问题讨论】:
-
你的 UIbutton 和 UIImageview 框架是什么?确保它是相同的,我认为 Button 没有得到 UiImageview 的确切框架...请检查一次...
-
我认为您缺少
:尝试将action: "clickProfile"替换为action: "clickProfile:" -
action: "clickProfile:" 对我也不起作用。
标签: ios swift uiimageview uibutton