I would like a way to either reduce the darkness change (or turn off as a last resort) that you get when you hold down a UIButton on iOS 7 and 6. It looks horrible due to the image on the button when selected
Thanks
Dylan
You can set different images and/or background images based on the UIControlState.
For example in case of images:
[comment_notification setBackgroundImage:[UIImage imageNamed:#"1"] forState:UIControlStateNormal];//Normal state
[comment_notification setBackgroundImage:[UIImage imageNamed:#"2"] forState:UIControlEventTouchUpInside];//pressed and released state
[comment_notification setBackgroundImage:[UIImage imageNamed:#"3"] forState:UIControlEventTouchDown];//pressed state
or in case of background colour, add targets:
[loginButton addTarget:self action:#selector(performLogin) forControlEvents:UIControlEventTouchUpInside];
[loginButton addTarget:self action:#selector(loginButtonPressed) forControlEvents:UIControlEventTouchDown];
[loginButton addTarget:self action:#selector(releasedButton:) forControlEvents:UIControlEventTouchUpOutside];
and in the target method, like buttonpressed:
- (void) buttonpressed:(UIButton*)button{
[button setBackgroundColor:[UIColor blueColor]];
}
and so on for respective states
Related
I want to add image as button, and when it is clicked it should open a new view controller...
Does anyone know how to do?
I created a button and added image, but its not nice, because when I click on the image it animates a square arround the image, which I do not want. This what I did.
- (void)setTutorialButtonImage
{
[self.tutorialButton setImage:[UIImage imageNamed:#"app_logo.png"] forState:UIControlStateNormal];
}
Does any one know how to do... just an image and when I click on it start a new controller
You can disable the highlighting effect by using
self.tutorialButton.adjustsImageWhenHighlighted = NO;
Also try setting the same image for selected and highlighted states too
[button setBackgroundImage:[UIImage imageNamed:#"app_logo.png"] forState: UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"app_logo.png"] forState: UIControlStateSelected];
For better look and feel you can apply insets on image using
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(x, y, width, height)];
[sampleButton setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
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.
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.
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.
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];