Glossy effect when clicking on UINavigationBar button - objective-c

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;

Related

Can't put subviews over MKMapview

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];

Shows Touch On Highlight for UIImage

The UIButton has a cool attribute that is called "Shows Touch On Highlight" that allows me to tell the user "you touched this button".
Is there any way I can implement this for a UIImage? I want the user to see the point touched inside the UIImage.
If you are using a little trick. make a transparent button above UIImageView. so must set a UIImageView userInteractionEnabled true bec default false. as follows:
imageView.userInteractionEnabled = YES;
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = imageView.frame;
clearButton.showsTouchWhenHighlighted = YES;
[clearButton setBackgroundColor:[UIColor clearColor]];
[imageView addSubView:clearButton];
other trick, create a transparent button, and set image your want.
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
clearButton.showsTouchWhenHighlighted = YES;
[clearButton setImage:[UIImage imageNamed:#"your_image"] forState:UIControlStateNormal];
[clearButton setBackgroundColor:[UIColor clearColor]];
No, it doesn't in same way as in UIButton, but you can use UIButton of type custom, instead to solve it for you.
Another help with UIImageView is setting it's Highlight image, but still you have to work for effect.

change the background image of button in objective-c?

I've a UIButton on cell of UITableView in my VC like this
arrowBtnUp = [UIButton buttonWithType:UIButtonTypeCustom];
arrowBtnUp.frame = CGRectMake(50,(65-19)/2,31, 31);
[arrowBtnUp setBackgroundImage:[UIImage imageNamed:#"arrow_up.png"] forState:UIControlStateNormal];
[arrowBtnUp addTarget:self action:#selector(slideAction) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:arrowBtnUp];
and this my slideAction
-(void) slideAction
{
[arrowBtnUp setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
// also tried with UIControlStateSelected/Highlighted
}
But it didn't work.I found this link but didn't help.
Any suggestion or sample would be appreciated.
Your code should work.Anyways change your code like this and try
-(void) slideAction:(id)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
}
and
[arrowBtnUp addTarget:self action:#selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
and ensure that you have arrow_down.png in your app bundle
The issue is you are reusing the arrowBtnUp instance of UIButton.
Hence in the slideAction method you won't get the pressed buttons reference.
For getting the reference in the slideAction method you need to pass it as an argument.
So change the code like:
[arrowBtnUp addTarget:self action:#selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
-(void) slideAction:(UIButton *)myButton
{
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
}
If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.
Background image is different than the icon on the button. Try this instead:
[_buttonConfirm setImage:[UIImage imageNamed:#"s.png"] forState:UIControlStateNormal];
If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_up.png"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateSelected];
There is no need to manually switch the background images in your slideAction: target method.

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.

iOS dynamic addition of UIControls

I'm trying to add a bunch of UIButtons to a screen at runtime. Based on user input I want to show different buttons in different locations. Is this possible?
Yes, it's possible.
Creating a button in objective-C:
UIButton *btnPrefix = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPrefix setFrame:CGRectMake(0, 0, 20, 20)];
[btnPrefix setUserInteractionEnabled: YES];
[btnPrefix addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: btnPrefix];