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];
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 want to display two buttons inside the popoverview. I am using WEPOPOVER, I am able to display one btn.
But I am not sure how to display the two buttons.
UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[editBtn setTitle:#"EDIT ME" forState:UIControlStateNormal];
[editBtn setFrame:CGRectMake(250, 0, 100,40)];
[editBtn addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
[editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
editBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
editBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
editBtn.backgroundColor = [UIColor blackColor];
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteBtn setTitle:#"Delete ME" forState:UIControlStateNormal];
[deleteBtn setFrame:CGRectMake(250, 0, 100,40)];
[deleteBtn addTarget:self action:#selector(doSubscription) forControlEvents:UIControlEventTouchUpInside];
[deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
deleteBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
deleteBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
deleteBtn.backgroundColor = [UIColor blackColor];
// place inside a temporary view controller and add to popover
UIViewController *viewCon = [[UIViewController alloc] init];
viewCon.view = (UIView *)[NSArray arrayWithObjects:editBtn,deleteBtn, nil];
viewCon.contentSizeForViewInPopover = editBtn.frame.size; // Set the content size
navPopover = [[WEPopoverController alloc] initWithContentViewController:viewCon];
[navPopover presentPopoverFromRect:editButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
animated:YES];
You have given the same frame for both buttons. So it shows just one button.
[editBtn setFrame:CGRectMake(250, 0, 100,40)];
[deleteBtn setFrame:CGRectMake(250, 0, 100,40)];
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!
Hi I have a UIButton that I created programmatically but unfortunately its not working. When I click on the UIButton nothing happens. I don't know what is the problem. Any help would be appreciated.
UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
[viewContainer addSubview:contentView];
return contentView;
- (void)buttonClicked
{
NSLog(#"button clicked");
}
I think labels recives touches instead of button.
Add userInteractionEnabled = NO; to your labels.
label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Try changing the top part of your code to this:
CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];
//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
It may very well be the following:
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
im sure you would need to add them in the following order:
[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];
This is because when you call addSubview: it adds it to end of the receivers list of subViews and the most recent subView appears on top.
Check out the UIView Documentation here
the reason that the Interaction isn't being detected is that you are not passing the touchEvents through to the other subViews as they are being captured by label1 and stopping there.
By default the last subView captures all touch events and doesn't pass them on.
Add the colon in last of your method in selector
[connectedStories addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:#"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:#"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];