Can't put subviews over MKMapview - objective-c

When I try to put a subview like UIButton or UILabel over MKMapView in the my xib file, the MKMapView is automatically replaced with the subview - I can't add subviews on MKMapView. How should I fix this problem? Thank you in advance for your answers!

You can add Subview on MKMapView,
Below is the code for adding a Button on MapView programatically (For your reference)
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 10, 120, 60);
[button setTitle:#"test" forState:UIControlStateNormal];
[mapView addSubview:button];

Related

TVOS: UIButton action not working in subview

in the TVOS Application storyboard mainview I added a subview.
Within the subview there are 3 button created in the very same way (the code here belongs to the subview)
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
b.frame = CGRectMake(left, top, 400, size);
[b setTitle:#"Click Me" forState:UIControlStateNormal];
[b addTarget:self action:#selector(Action_Up) forControlEvents:UIControlEventPrimaryActionTriggered];
[self.view addSubview:b];
...
-(void)Action_Up {
[self ShowData];
}
in the simulator and on the real device I can select the button and press it, but the action is never fired.
Could you help ?
Try this.. it worked for me
button.addTarget(self, action: "someMethodName:", forControlEvents: .PrimaryActionTriggered)

setting background on UIButton on Map Callout

Hi I am trying to set a background image on my detail disclosure UIButton attached to my MapView MKAnnotation callout view. Checking on stackoverflow there are consistent examples of how to set this, however it doesn't seem to work for my callout UIButton. My question is, does this not work, simply because of the mapview or am I missing something else?
Here is my code:
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"Tch4"];
//pinView.calloutOffset = CGPointMake(0, 32);
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImage *btnImage = [UIImage imageNamed:#"4stars"];
[pinView.rightCalloutAccessoryView setBackgroundImage:btnImage forState:UIControlStateNormal];
I receive the error on the final line. Error is: No Visable interface for UIView declares the selector 'setbackgroundimage: forstate'
Thanks for your help with this
Your problem is rightCalloutAccessoryView returns a UIView instance. And UIView does not respond to setBackgroundImage:forState: method.
What you are looking for is to add a UIButton instead something like this:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(0, 0, 32, 32); // Set your own frame
[infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[infoButton setBackgroundImage:btnImage forState:UIControlStateSelected];
pinView.rightCalloutAccessoryView = infoButton;
you cant set background image if your typebutton DetailDisclosure.
you can addsubview image to rightCalloutAccessoryView but without button..
so if you still use typebutton DetailDisclosure you cant set background

Title in UIButton with Image

I have set an image in a UIButton but I need to set the title above this image. How can I do this?
[button setImage:[UIImage imageNamed:#"btn_select_s.png"] forState:0];
[button setImage:[UIImage imageNamed:#"btn_select_p.png"] forState:1];
[button setTitle:#"my title" forState:0];
You can use UIButton's titleEdgeInsets and imageEdgeInsets to position title and image as you want.
See Apple's UIButton reference documentation
For example, in a UIButton subclass:
[self setImageEdgeInsets:UIEdgeInsetsMake(imageFrame.origin.y, imageFrame.origin.x, 0, 0)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(labelFrame.origin.y, labelFrame.origin.x, 0, 0)];
You need to set the background image of the button instead:
[button setBackgroundImage:[UIImage imageNamed:#"foo"] forState:UIControlStateNormal];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
//Use you values for edge insets here.
[button setTitleEdgeInsets:UIEdgeInsetsMake(20.0f, 10.0f, 0.0f, 0.0f)];
There is a convenient UIButton subclass here:
https://github.com/lionhylra/FlexButton
It has features like:
- It provides 4 layout styles.
- It works as simple as UIButton.
- When you adjust the size of button, the size of imageView and titleLabel will change accordingly and correctly. They will not be put into disorder. This is the essential difference to using edge inset.
- TitleLabel and imageView will always be centered vertically and horizantally.

Glossy effect when clicking on UINavigationBar button

When I'm clicking on a UIButton that located on the top of UINavigationBar I have this glossy effect:
How can I add this effect to another button that located on a UINavigatonBar but not as a left or right button item? like this:
UIButton * emptyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[emptyButton setFrame:CGRectMake(119, 5, 82, 35)];
[emptyButton addTarget:self action:#selector(centerNavigationBarTitleClicked:) forControlEvents:UIControlEventTouchUpInside];
[emptyButton setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar addSubview:emptyButton];
Thanks!
You simply have to set the showsTouchWhenHighlighted property to YES to get the glow effect, i.e.
emptyButton.showsTouchWhenHighlighted = YES;

UIButton tap not responding

I added a new UIButton
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setFrame:CGRectMake(x, y, width, height)];
[newButton setTitle:#"" forState:UIControlStateNormal];
[newButton setTag:x+INDEX_OFFSET];
[newButton setBackgroundImage:image forState:UIControlStateNormal];
[newButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:newButton];
[self bringSubviewToFront:newButton];
and I can see it on the screen but it is not responding to a tap gesture. It should call "buttonPressed". Any help would be super!
One of the possible problem is that you are adding the button in a frame position out its superview. Just to check it easily try to set clipToBounds property to YES in its superview bounds, then run the app. If you don't see the button it means that you set the button position out superivew position, that's why it doesn't respond to touches.
Another possible problem is if you add the UIButton as a subview to an UIImageView (which you shouldn't, but it works). In this case, you could either add it to a UIView or set UIImageView's userInteractionEnabled property to YES.