简体   繁体   English

当UIButton位于MapView顶部时,不可单击

[英]UIButton is not clickable when it's on top of a MapView

i've searched for quite a bit of time but so far I couldn't find the exact problem. 我已经搜索了很多时间,但到目前为止我找不到确切的问题。

I've created a mapView in InterfaceBuilder. 我已经在InterfaceBuilder中创建了一个mapView。 Trough the click of a button I add another View. 通过单击按钮,我添加了另一个视图。

self.buttonView = [[ButtonView alloc] init];
self.buttonView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.buttonView];

In the created buttonView, I create 5 buttons programmatically, like this: 在创建的buttonView中,以编程方式创建5个按钮,如下所示:

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(25, 275, 100, 30);
[button1 setTitle:@"button1!" forState:UIControlStateNormal];
[self addSubview:button1];

The buttons appear correctly, but they are not clickable. 这些按钮正确显示,但是不可单击。 I've tried several things,but nothing seems to work so far. 我已经尝试了几件事,但是到目前为止似乎没有任何效果。 Whenever I click on a button, the mapView gets clicked. 每当我单击按钮时,都会单击mapView。 For example when I double click a button, the mapView zooms in. Both mapView and buttonView are subViews of "view". 例如,当我双击一个按钮时,mapView会放大。mapView和buttonView都是“视图”的子视图。

I want the buttonView to be on top of the mapView (which it does) and the mapView to scroll underneath the buttonView (which it does), while the buttons stay where they are (which they do), but they are not clickable 我想将buttonView放在mapView的顶部(它确实要这样做),并且让mapView滚动到buttonView的下方(它确实要这样做),而按钮却保持在它们所在的位置(它们确实如此),但是它们不可单击

Help is greatly appreciated! 非常感谢您的帮助! :-) :-)

EDIT1: Tried self.buttonView.userInteractionEnabled = YES; EDIT1:尝试过self.buttonView.userInteractionEnabled = YES; - doesn't change anything. -没有任何改变。 (also for the buttons themselves! (也适用于按钮本身!

一个非常简单的错误,我的“上方”视图不可见,并且拒绝单击:S下方的按钮

I used interface builder and was not seeing the button. 我使用了界面生成器,但没有看到该按钮。 I had to set the MKMapView to hidden until the button was displayed and in the right position. 我必须将MKMapView设置为隐藏,直到显示按钮并将其放置在正确的位置。 Then I set the MKMapView to visible. 然后,将MKMapView设置为可见。

Here are the steps I had to take in the interface builder to get my button to show 这是我必须在界面生成器中执行的步骤,才能显示我的按钮

  1. Create a UIViewController which contains a view 创建一个包含视图的UIViewController
  2. Add an MKMapView and set constraints 添加一个MKMapView并设置约束

在IB的视图控制器中显示MKMapView

  1. Add UIButton in the same view as the MKMapView, set image and text, whatever is applicable. 在适用的情况下,在与MKMapView相同的视图中添加UIButton,设置图像和文本。 If MKMapView disappear when you try to resize or move the button, the map view has just set its width and height to zero. 如果在尝试调整按钮大小或移动按钮时MKMapView消失,则地图视图会将宽度和高度设置为零。 Enter new values in the Size Inspector for the map view,and it will resize back. 在“大小”检查器中为地图视图输入新值,它将重新调整大小。 Or you can move the button elsewhere off the MKMapView and then move the button back to where you want it. 或者,您可以将按钮移到MKMapView的其他位置,然后将按钮移回所需位置。

  2. The important part is to set constraints for the UIButton. 重要的部分是设置UIButton的约束。 I set the MKMapView hidden until I got the size and placement right and then set it back to visible. 我将MKMapView设置为隐藏,直到获得正确的大小和位置,然后将其设置回可见。

    在IB中的约束下,在MKMapView顶部显示一个UIButton

Here's how it looks in the simulator 这是模拟器中的外观

显示正在运行的应用程序,其中有地图上的按钮

Not sure it is a typo error: You should add subview to self.buttonView right? 不确定这是一个错字错误:您应该将子视图添加到self.buttonView中,对吗?

[self.buttonView addSubview:button1]

See it helps 看到它有帮助

I had a bit similar case once, and the solution was 我曾经有一个类似的案例,解决方案是

[button1 setExclusiveTouch:YES];

If the view under a control element also handles touch events, the control might work in unexpected ways. 如果控件元素下的视图也处理触摸事件,则控件可能以意外的方式工作。

Using Cartogaphy I also had to set the height and width constraints similar to @Maria. 使用Cartogaphy,我还必须设置类似于@Maria的高度和宽度约束。 So the following would not work: 因此,以下操作将无效:

categoryButton.addTarget(self, action: "toggleCategoryDropdown:", forControlEvents: UIControlEvents.TouchUpInside)
categoryButton.backgroundColor = UIColor.purpleColor()
categoryButton.userInteractionEnabled = true

self.addSubview(categoryButton)

constrain(categoryButton) {
    categoryButton in

    categoryButton.top == categoryButton.superview!.top + 26
    categoryButton.left == categoryButton.superview!.left + globalConfig.margin
}

But this did work 但这确实有效

categoryButton.addTarget(self, action: "toggleCategoryDropdown:", forControlEvents: UIControlEvents.TouchUpInside)
categoryButton.backgroundColor = UIColor.purpleColor()
categoryButton.userInteractionEnabled = true

self.addSubview(categoryButton)

constrain(categoryButton) {
    categoryButton in

    categoryButton.top == categoryButton.superview!.top + 26
    categoryButton.left == categoryButton.superview!.left + globalConfig.margin
    categoryButton.width == 100
    categoryButton.height == 100
}

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

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