Ios UIImageView上的按钮不可单击
我以编程方式创建Ios UIImageView上的按钮不可单击,ios,swift,uiimageview,uibutton,Ios,Swift,Uiimageview,Uibutton,我以编程方式创建UIImageView,并在其上添加一个UIButton。但是按钮是不可点击的 当我点击按钮我的账户,它将显示为第二张照片。此时将显示UIImageView,上面有一个按钮My profile。但是当我点击我的个人资料时,它不起作用。当我再次单击My account时,UIImageView将像我预期的那样消失(My profile也将消失) 当将操作:“clickProfile”修改为操作:“clickProfile:时,它仍然不起作用。当我单击按钮myAccount时,将显示
UIImageView
,并在其上添加一个UIButton
。但是按钮是不可点击的
当我点击按钮我的账户
,它将显示为第二张照片。此时将显示UIImageView
,上面有一个按钮My profile
。但是当我点击我的个人资料时,它不起作用。当我再次单击My account
时,UIImageView
将像我预期的那样消失(My profile
也将消失)
当将操作:“clickProfile”
修改为操作:“clickProfile:
时,它仍然不起作用。当我单击按钮myAccount
时,将显示UIImageView
,当我单击UIImageView
上的按钮时,它将打印“工作”。当我单击我的帐户时,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上的按钮仍然不起作用。它是如何发生的?您的选择器视图alpha为0,hidden为true。移除这些
picker.alpha = 0
picker.hidden = true
台词
更新此行
buttonProfile.addTarget(self, action: "clickProfile", forControlEvents: UIControlEvents.TouchUpInside)
到
不建议在图像视图中添加子视图。即使这样做,如果图像视图被隐藏,则其所有子视图也将被隐藏。当视图被隐藏时,用户交互不会以任何方式发生 您应该将按钮添加到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,使其显示在屏幕上。您可以根据自己的要求进行定位。尝试记录选择器和按钮框架 根据您的代码,这些控件的框架稍微关闭,您的按钮框架应该位于选择器内部
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)
假设您在分辨率为320 x 568的iPhone 5 sim/设备上运行此功能
让我们假设您的self.view
框架与此相同
因此,self.view
中的选择器框是(17200286290),选择器坐标中的按钮框是(140,-180260,43),当转换为self.view
坐标时变成(157,20260,43)
这些坐标的视觉表示如下所示。
如您所见,您的按钮(绿色图片)不在它应该位于的位置,请尝试修改按钮框,使按钮位于选择器(红色图片)内。UIbutton和UIImageview的框是什么?请确保它是相同的,我认为按钮没有得到UiImageview的精确框架…请检查一次…我认为您缺少
:
尝试将操作:“clickProfile”
替换为操作:“clickProfile:
操作:“clickProfile:”对我也不起作用。不。我需要选择器是透明的。并将操作:“clickProfile”修改为操作:“clickProfile:”仍然不适用于我。当我单击另一个按钮myAccount时,UIImageView应该会出现。当我再次单击myAccount时,它应该消失。使用picker.backgroundColor=UIColor.clearColor()使其透明,picker.hidden并不总是为false。当我单击“我的帐户”按钮时,它将为真。我只是更新我的代码。我想对按钮“我的帐户”实现下拉菜单效果,因此我需要设置UIImageView在单击“我的帐户”时也消失和显示。我的按钮偏移量在屏幕上工作(我使用的模拟器是iphone6,它在我期望的位置上),你能告诉我如何对这些坐标进行视觉表示吗?如何记录框架?我甚至不知道如何在网上搜索。我试图将UIImageView移动到右上角的控制器,以便按钮可以位于UIImageView内部,但我不知道UIImageView的x,y值应该是多少。(不移动按钮,但移动UIImageView)您可以使用此打印(“%@”,NSStringFromCGRect(picker.frame)记录帧
。至于可视化表示,我正在使用游乐场并使用这些框架创建控件。你能告诉我如何使用这些框架创建控件吗?我创建了一个游乐场并调用类或这些子函数,但它不显示结果。你能告诉我如何调用类吗?可以让它像你一样显示可视化表示吗?尝试遵循这些教程。
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)