background blue button image obj c - objective-c

I'm assigning to a button two different types of image..
something happens to me that I can't understand .. it creates a sort of background for the blue image
[self.shippingButton setBackgroundImage:[UIImage customImageWithName:#"terms_and_conditions_unselected"] forState:UIControlStateNormal];
[self.shippingButton setBackgroundImage:[UIImage customImageWithName:#"terms_and_conditions_selected"] forState:UIControlStateSelected];
[self.billingButton setBackgroundImage:[UIImage customImageWithName:#"terms_and_conditions_unselected"] forState:UIControlStateNormal];
[self.billingButton setBackgroundImage:[UIImage customImageWithName:#"terms_and_conditions_selected"] forState:UIControlStateSelected];
- (IBAction)shippingButtonChanged:(id)sender {
self.shippingButton.selected = !self.shippingButton.selected;
}
- (IBAction)billingButtonChanged {
self.billingButton.selected = !self.billingButton.selected;
}
enter image description here
upload image

Related

how to fit image inside a circular button objective c

I created a button in storyboard with a circular form using layer.cornerRadius in the key path. I want to add an image to it. How can I add an image to my button and the image is fit the same size of my button
Use this to set image size to adjust button size:
[btn setImage:[UIImage imageNamed:#"image_name"] forState:UIControlStateNormal];
btn.imageView.contentMode = UIViewContentModeScaleAspectFit; //set to fit button size
It should be like this
UIImage *btnImage = [UIImage imageNamed:#"image.png"];
[buttonName setImage:btnImage forState:UIControlStateNormal];
Write the below code in viewDidAppear instead of viiewDidLoad
buttonName.layer.masksToBounds = YES;
buttonName.layer.cornerRadius = 20;//half of the button height
Try this to fit image to Button :
UIButton *button =[[UIButton alloc]initWithFrame: CGRectMake(10, 10, 50, 32)];
[button setImage:[UIImage imageNamed:#"image_name"] forState:UIControlStateNormal];
// to set image to fit button size
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
//ScaleToFill (UIViewContentModeScaleToFill)
//ScaleAspectFit (UIViewContentModeScaleAspectFit)
//ScaleAspectFill (UIViewContentModeScaleAspectFill)

UIButton - turning off or reduing "selection/hold down" tint colour

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

How to add image as button in objective c?

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

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.

How to change the image of the button on click of the button in objective c

I have placed a pause button in UI, where when the user clicks the button, the image of button from pause image it has to change to the start image. I have placed both the start and the pause images in the bundle, I am able to do this,but when i click on start again , it should show pause image button,How to do this, the following is the code i am using,
-(IBAction)btnClked:(id)sender
{
[pauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
You need a variable to keep track of that state change. A BOOL is fine.
But using your code, you can "abuse" the tag property of UIButton to tell if it's playing (tag == 1) or paused (tag == 0)
-(IBAction)btnClked:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.tag == 1) {
btn.tag = 0;
[btn setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
} else {
btn.tag = 1;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
}
You can use UIButton's selected state to do the same. For example, while initiating the button you have to define 2 images. One for normal state and other for selected state. When touch event of the button fired, you have to make it selected. If it is already selected you need to deselect it. This is much better, than maintaining additional variables to check selected state of the button. Also you could improve the performance with this approach.
Code example
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[startButton setBackgroundImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[startButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
}
- (IBAction)modeButtonClick:(id)sender {
UIButton *startButton = (UIButton *)sender;
[startButton setSelected:![startButton isSelected]];
}
If you are creating the button from story board or XIBs set the backgroud image for default state config and selected state config.