我以编程方式创建UIImageView
,并在其上添加一个UIButton
。但该按钮无法点击。
当我点击按钮My account
时,它将显示为第二张照片。 UIImageView
会显示,其上有一个按钮My profile
。但是当我点击它时My profile
不起作用。当我再次点击My account
时,UIImageView
会像我预期的那样消失(MY profile
也会消失)
将[{1}}修改为action: "clickProfile"
时,它仍然无效。当我点击按钮action: "clickProfile:"
时,myAccount
会显示,当我点击UIImageView
上的按钮时,它会打印出“工作”。单击UIImageView
时,UIImageView将消失。
这是我的所有代码:
myAccount
我已经启用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
}
)
}
}
为真,但UIImageView上的按钮仍然不起作用。它是如何发生的?
答案 0 :(得分:1)
不建议在图像视图中添加子视图。即使你这样做,如果图像视图被隐藏,那么它的所有子视图也将被隐藏。隐藏视图时,无论如何都不会发生userInteraction。
您应该将按钮添加到self.view并将其放在图像视图的顶部,而不是将其放在图像视图中。试试这段代码。
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
picker.clipsToBounds = false
self.view.addSubview(picker)
let offset = 100
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: "myAccount:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(buttonProfile)
}
按钮的偏移也在屏幕外。我将其设置为100以使其显示在屏幕上。您可以根据自己的要求定位。
答案 1 :(得分:1)
尝试记录选择器和按钮框架。
从您的代码中,这些控件的框架稍微关闭,您的按钮框架应位于拾取器内。
picker.frame = CGRect(x: ((self.view.frame.width / 2) - 143), y: 200, width: 286, height: 291)
buttonProfile.frame = CGRect(x: 140, y: -180, width: 260, height: 43)
假设您在iPhone 5 SIM /设备上运行此分辨率为320 x 568。
假设您的self.view
框架与此相同。
因此,self.view
中的选择器框架为(17,200,286,290),并且拾取器坐标中的按钮框架为(140,-180,260,43),并且转换为self.view
时坐标变为(157,20,260,43)。
正如您所看到的,您的按钮(绿色图片)不在原处,尝试修改按钮框,以便按钮位于您的选择器内(红色图片)。
答案 2 :(得分:0)
您的选择器视图alpha为0且隐藏为true。删除这些
picker.alpha = 0
picker.hidden = true
行。
更新此行
buttonProfile.addTarget(self, action: "clickProfile", forControlEvents: UIControlEvents.TouchUpInside)
到
buttonProfile.addTarget(self, action: "clickProfile:", forControlEvents: UIControlEvents.TouchUpInside)