简体   繁体   中英

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

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. For example when I double click a button, the mapView zooms in. Both mapView and buttonView are subViews of "view".

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

Help is greatly appreciated! :-)

EDIT1: Tried 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. Then I set the MKMapView to visible.

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
  2. Add an MKMapView and set constraints

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

  1. Add UIButton in the same view as the MKMapView, set image and text, whatever is applicable. If MKMapView disappear when you try to resize or move the button, the map view has just set its width and height to zero. 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.

  2. The important part is to set constraints for the UIButton. I set the MKMapView hidden until I got the size and placement right and then set it back to visible.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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