简体   繁体   English

UIButton没有注册触摸

[英]UIButton is not registering touches

I added UIButton with custom image and no text to Navigation Bar in one of my View Controllers. 我将具有自定义图像且没有文本的UIButton添加到了其中一个View Controller中的导航栏中。 In that Navigation bar is also default button Back. 在该导航栏中也是默认按钮“上一步”。

I connected that UIButton with Connection: Action, Type: UIButton, Event: Touch Up Inside and Arguments: Sender. 我将该UIButton与Connection:动作,类型:UIButton,事件:Touch Up Inside和Arguments:Sender连接在一起。

When I click on that button nothing happens, it's not registering touch at all. 当我单击该按钮时,什么也没有发生,这根本没有记录触摸。 Button Back is working normally. “后退”按钮正常工作。

在此处输入图片说明

Why is this happening? 为什么会这样呢? How to fix it? 如何解决?

Try this: 尝试这个:

  1. Since you are adding it to navigation bar it must be a UIBarButtonItem . 由于您要将其添加到导航栏中,因此它必须是UIBarButtonItem So the action outlet you are connecting to it must have a type UIBarButtonItem and not UIButton . 因此,您要连接的操作出口必须具有UIBarButtonItem类型,而不是UIButton类型。

  2. Check if the outlet of the UIBarButtonItem is connected properly. 检查UIBarButtonItem的插座是否正确连接。

  3. Check if the user interaction of UIBarButtonItem is enabled. 检查是否启用了UIBarButtonItem的用户交互。

When I click on that button nothing happens, it's not registering touch at all. 当我单击该按钮时,什么也没有发生,这根本没有记录触摸。

99% of the time when I see this complaint, the reason is that you've somehow placed the button outside its superview. 我有99%的时间看到此投诉,原因是您已将按钮以某种方式置于其超级视图之外。 A button outside its superview is visible (by default) but not touchable. 其视图外部的按钮是可见的(默认情况下),但不可触摸。 (Sometimes this happens because the superview itself has never been given a size and so has zero size; other times it is for some other reason.) (有时发生这种情况是因为超级视图本身从未被赋予大小,因此其大小为零;有时是由于其他原因。)

I can't tell you any more because you didn't tell any more (ie you didn't give details of how this button is getting into the navigation bar). 我无法再告诉您,因为没有再告诉您(即您没有提供此按钮如何进入导航栏的详细信息)。 But I'm willing to bet that if you dig deeper you'll find that my guess is right. 但是我敢打赌,如果您深入研究,您会发现我的猜测是正确的。

我通过先将导航项添加到导航栏,然后将按钮添加到导航项来解决它。

To add button on navigation bar you have to add like this: 要在导航栏上添加按钮,您必须像这样添加:

 let leftButton = UIBarButtonItem(title: "abc", style: .Plain, target: self, action: #selector(ViewController.buttonClicked(_:)))
 let navigationItem = UINavigationItem()
 navigationItem.titleView = UIImageView(image: UIImage(named: "image.png"))
 navigationItem.leftBarButtonItem = leftButton

This is for left button. 这是用于左键。 You can add right button in the same way. 您可以以相同的方式添加右键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM