当UIButton位于MapView之上时,它是不可点击的

时间:2012-04-25 00:57:29

标签: iphone ios uibutton mkmapview clickable

我搜索了相当多的时间,但到目前为止我找不到确切的问题。

我在InterfaceBuilder中创建了一个mapView。通过单击按钮,我添加了另一个视图。

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

在创建的buttonView中,我以编程方式创建了5个按钮,如下所示:

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

按钮显示正确,但无法点击。我尝试了几件事,但到目前为止似乎没有任何工作。 每当我点击一个按钮时,都会点击mapView。例如,当我双击一个按钮时,mapView会放大.mapView和buttonView都是“view”的子视图。

我希望buttonView位于mapView(它可以)的顶部,mapView在buttonView(它可以)下面滚动,而按钮保持原样(他们这样做),但它们不是可点击

非常感谢帮助! : - )

EDIT1:尝试self.buttonView.userInteractionEnabled = YES; - 不会改变任何内容。 (也是按钮本身!

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我使用了界面构建器而没有看到按钮。我必须将MKMapView设置为隐藏,直到按钮显示并处于正确位置。然后我将MKMapView设置为可见。

以下是我必须在界面构建器中执行以使我的按钮显示

的步骤
  1. 创建一个包含视图的UIViewController
  2. 添加MKMapView并设置约束
  3. shows a MKMapView in a view controller in IB

    1. 在与MKMapView相同的视图中添加UIButton,设置图像和文本,无论适用的是什么。如果尝试调整大小或移动按钮时MKMapView消失,则地图视图刚刚将其宽度和高度设置为零。在地图视图的“大小”检查器中输入新值,它将调整大小。或者您可以将按钮移动到MKMapView的其他位置,然后将按钮移回到您想要的位置。

    2. 重要的是为UIButton设置约束。我将MKMapView设置为隐藏,直到我获得正确的大小和位置,然后将其设置为可见。

      shows a UIButton on top of the MKMapView with constraints in IB

    3. 以下是它在模拟器中的外观

      shows the running app with button on to of map

答案 2 :(得分:0)

不确定这是错字错误:您应该将子视图添加到self.buttonView吗?

[self.buttonView addSubview:button1]

看到它帮助

答案 3 :(得分:0)

我有一次类似的案例,解决方案是

[button1 setExclusiveTouch:YES];

如果控制元素下的视图也处理触摸事件,则控件可能会以意想不到的方式工作。

答案 4 :(得分:0)

使用Cartogaphy我还必须设置类似于@Maria的高度和宽度约束。所以以下方法不起作用:

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.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
}