I want to change an image on my UIButton when a user presses on it. The image has transparency effects (it is a .png that shows only an edge). The problem is that when the button is tapped, all works fine (other functions connected to it), but the app overwrites the 2 different images one over the second! When the button is pressed I want to show the second image, and remove the first (that becomes unnecessary). PS: I don't want to use array of images. Here the code for the button:
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //just graphics
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside]; //just to set a variable i++ for my cycle
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside]; //function that changes another shape
[[self view] addSubview:overlayButton];
my simple cycle
- (void)buttonshapeClicked:(id)sender{
shape++;
if (shape == 4) {
shape = 1;
}}
here the main function that allows to change a primary shape on the screen
-(void) changeshape:(id)sender {
if (shape==1 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"passaquadrato.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:#"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"circle2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}
else if (shape==2 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"passarettangolo.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:#"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"square2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}}
I want to have the same button but with a different image to show
It doesn't look like you are ever removing the previous buttons.
Also, you don't have to create new buttons - you can simply store a reference to the button, and change the image, and change the action target (by removing the old ones before adding the new ones).
I've solved for a few of cycle with removing all from the view with:
for (int i = 0; i < [[self.view subviews] count]; i++ ) {
[[[self.view subviews] objectAtIndex:i] removeFromSuperview];
}
The problem now is to reinitialize the cycle because, when my variable goes to 4 I want that it becomes 1. I do it with:
- (void)buttonshapeClicked:(id)sender{shape++;
if (shape == 4) {
shape = 1;
}}
But after the number 4, I've the same problem of overview that I've had before!
Related
I have the following to add a custom image as a back button. The problem is that it overrides the default navigation controller back method.
How can I correct this?
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"button-back-arrow.png"] forState:UIControlStateNormal];
//[button addTarget:self action:#selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 40, 29)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
thanks for any help
just add this line and method..
[button addTarget:self
action:#selector(BtnBack_Clicked:)
forControlEvents:UIControlEventTouchDown];
and call this method
-(IBAction)BtnBack_Clicked:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 84, 31)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 84, 31);
[btn setBackgroundImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(action) forControlEvents:UIControlEventTouchUpInside];
[btnView addSubview:btn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnView];
[btnView release];
I am creating a UIButton programmatically as below.
- (void)viewDidLoad
{
[paymentsHomeView setFrame:CGRectMake(0, 0, 320, 520)]; // paymentsHomeView is added and referenced in IB
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
[super viewDidLoad];
}
-(void) buttonClicked {
NSLog(#"buttonClicked");
}
There is no output for the above code. The button is not created.
Whereas the same works when I create UITextField.
Help me out. Thanks in advance..!
you need to set frame for button after init statement and you need to set title for button in different way
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
Try this one my friend..
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[paymentButton setTitle:#"Show View" forState:UIControlStateNormal];
paymentButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.paymentsHomeView addSubview:paymentButton];
let me know is it working or not...!!!
Happy Coding!!!!
You are creating button here:
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
and again on this line you create another button so this overwrite the previous one, so you need to set the frame again:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
What I suggest do this:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton setFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
I have notated some problems here:
[paymentsHomeView setFrame:CGRectMake(0, 0, 320, 520)]; // paymentsHomeView is added and referenced in IB
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
// You just allocated a paymentButton above. Without ARC it is leaked here:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Now you have assigned an auto released button to paymentButton. It does not have a frame or text.
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
Try this:
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
paymentButton.frame = CGRectMake(10, 45, 300, 41);
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
For Custom Button use bellow code...
Remove this line
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and add this line which shows button with black background
paymentButton.titleLabel.textColor = [UIColor whiteColor];// set text color which you want
[paymentButton setBackgroundColor:[UIColor blackColor]];// set back color which you want
And for RoundRect Button use bellow code
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
paymentButton.frame = CGRectMake(10, 45, 300, 41);
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
[self.paymentsHomeView bringSubviewToFront:paymentButton];
I am new to iPhone,
I want to implement radio button in my app, there are three buttons in my app, button should behave like radio button.
Here is my code snippet,
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
- (IBAction)RadioButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Any help will be appreciated.
You can use following code.
//Add all the buttons to class level NSMutable array
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
[self.buttonsArray addObject:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
[self.buttonsArray addObject:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
[self.buttonsArray addObject:Btn3];
- (IBAction)RadioButton:(id)sender{
[self resetAllButtons];
UIButton* button=(UIButton*) sender;
[button setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateNormal];
}
-(void) resetAllButtons{
for(int i=0;i<[self.buttonsArray count];i++){
UIButton* button=[self.buttonsArray objectAtIndex:i];
[button setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
}
}
You are checking all views in self.view.subviews but the buttons are actually added to the UIScrollView (I guess) called scrollVw.
Change the for loop to
for (UIButton *Radiobutton in [self.scrollVw subviews]) {
if the scrollVw is added as a property in the .h file.
Any opposition to using a UISegmentedControl? It works like a radio button, in that it can only have one option selected at a time. If you are deadset on the appearance of radio buttons I would subclass it to change the appearance.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
Alternatively you could simply use the iOS provided Control for something like that:
UISegmentedControl
It can easily handle three segments, is quite flexible and can behave like a radio button where you can select only one segment or multiple.
I want to change an image on my UIButton when a user presses on it. The image has transparency effects (it is a .png that shows only an edge). The problem is that when the button is tapped, all works fine (other functions connected to it), but the app overwrites the 2 different images one over the second! When the button is pressed I want to show the second image, and remove the first (that becomes unnecessary). PS: I don't want to use array of images. Here the code for the button
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //just graphics
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside]; //just to set a variable i++ for my cycle
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside]; //function that changes another shape
[[self view] addSubview:overlayButton];
my simple cycle
- (void)buttonshapeClicked:(id)sender{
shape++;
if (shape == 4) {
shape = 1;
}}
here the main function that allows to change a primary shape on the screen
-(void) changeshape:(id)sender {
if (shape==1 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"passaquadrato.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:#"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"circle2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}
else if (shape==2 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"passarettangolo.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:#"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"square2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}}
Try like this:
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(setRightActionToButton:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
-(void)setRightActionToButton:(id)sender {
UIButton *button = (UIButton *)sender;
if (shape==1 && people==2) {
[button setImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
[self scanButtonPressed:button]
}
else if (shape==2 && people==2) {
[button setImage:[UIImage imageNamed:#"image3.png"] forState:UIControlStateNormal];
[self buttonshapeClicked:button]
}
else if (shape==3 && people==2) {
[button setImage:[UIImage imageNamed:#"image4.png"] forState:UIControlStateNormal];
[self changeshape:button];
}
}
I have a subview that has been added to my UIView. The idea is that the subview is a hovering panel with uibuttons. The subview is given an alpha of 0.2, as the user needs to be able to see what is behind it.
The problem is that when I add uibuttons to the subview, they inherit the alpha from the subview (I want the buttons to be non-transparent and have an alpha of 1.0). I've tried to tackle this problem by iterating through the uibuttons and setting their alpha back to 1.0 without success. Solutions?
// Create the subview and add it to the view
topHoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
topHoverView.backgroundColor = [UIColor blackColor];
[topHoverView setAlpha:0.2];
[self.view addSubview:topHoverView];
// Add button 1
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(20, 10, 80, 30)];
[button1 setTitle:#"New" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button1];
// Add button 2
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(120, 10, 80, 30)];
[button2 setTitle:#"Clear" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button2];
// Add button 3
button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setFrame:CGRectMake(220, 10, 80, 30)];
[button3 setTitle:#"Delete" forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:d button3];
// Attempt to iterate through the subviews (buttons) to set their transparency back to 1.0
for (UIView *subView in topHoverView.subviews) {
subView.alpha = 1.0;
}
You should create topHoverView with alpha 1.0 and a transparent background color. Then add a subview with a black background that covers the complete view with alpha 0.2 and then add the buttons to topHoverView:
// Create the subview and add it to the view
topHoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
topHoverView.backgroundColor = [UIColor transparentColor];
[topHoverView setAlpha:1.0];
[self.view addSubview:topHoverView];
canvasView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
canvasView.backgroundColor = [UIColor blackColor];
[canvasViewView setAlpha:0.2];
[topHoverView.view addSubview:canvasView];
// Add button 1
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(20, 10, 80, 30)];
[button1 setTitle:#"New" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button1];
// Add button 2
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(120, 10, 80, 30)];
[button2 setTitle:#"Clear" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button2];
// Add button 3
button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setFrame:CGRectMake(220, 10, 80, 30)];
[button3 setTitle:#"Delete" forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:d button3];
// Attempt to iterate through the subviews (buttons) to set their transparency back to 1.0
for (UIView *subView in topHoverView.subviews) {
subView.alpha = 1.0;
}
Instead of
topHoverView.backgroundColor = [UIColor transparentColor];
, the following code is right.
topHoverView.backgroundColor = [UIColor clearColor];