Im trying to add a uibarbutton- Camera style to a uitableviewcell programmatically like so
but I dont see anything in the uitableviewcell
UIButton *newBtn=[UIButton buttonWithType:UIBarButtonSystemItemCamera];
[newBtn setFrame:CGRectMake(250,10,50,30)];
[newBtn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[newBtn setEnabled:YES];
[cell addSubview:newBtn];
but when I use this code, I see a rounded rect button in the uitableviewcell
UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[newBtn setFrame:CGRectMake(250,10,50,30)];
[newBtn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[newBtn setEnabled:YES];
[cell addSubview:newBtn];
Is there any way I can add a uibarbuttonstyle button or all I can do is add a uibutton rounded rect style to the uitableviewcell?
UIBarButtonItem is not the same as UIButton. It doesn't even inherit from UIView, so you can't add it as a subview to other views. What happens when you call
[UIButton buttonWithType:UIBarButtonSystemItemCamera]
is that the UIBarButtonSystemItemCamera constant is of an invalid value for buttonWithType:, so UIButton can't decide which type of button to return - probably it just returns an empty, not configured one or nil.
So there's no easy solution to your problem. What you can do is, for example, take a screenshot of the Camera bar button item, then extract its image using some kind of image editor application, then set that image as the image of a custom UIButton like this:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"Camera.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:btn];
Related
When I click on the UIButton nothing happened
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create button like below.
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; // this is important
checkbox.frame = CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create the button first using button property and then give its frame.
If still its not working then self.background addSubview should be self.view addSubview.
What is self.background?
Your code is perfectly fine. You need to check the following .
check whether self.background is placed properly in your view heirarchy, I tried your code with self.view instead of self.background like below and it works perfectly.
self.checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 50, 50)];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox1.png"] forState:UIControlStateNormal];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox2.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.checkbox];
From the error message that you have given
"
Warning: Attempt to present <enter_the_den: 0x7fbf1bd4c4e0> on <ViewController: 0x7fbf1bc771c0> whose view is not in the window hierarchy!
I suspect the issue is in your code inside the method autologin, just comment all the code in your method and just try a log , it should work.
refer this : whose view is not in the window hierarchy
If you read the Apple Documentation for UIButton and look under the heading marked as Creating Button you'll find that the way to create a UIButton is to use buttonWithType: and not initWithFrame:.
Return Value
A newly created button.
Discussion
This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
The only time when you should use alloc/init with a UIButton is when you have subclassed UIButton however your init method should then implement the buttonWithType: and pass in UIButtonTypeCustom.
So change:
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
To
// Create a button of type custom
checkbox = [UIButton buttoWithType:UIButtonTypeCustom];
// Now set the frame for your button
[checkbox setFrame:CGRectMake(73, 324, 15, 15)];
// Continue with the rest of your button code
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
Check userInteractionEnabled property of your background property.
But i see why you have no feedback on button if rest is fine - you use UIControlStateSelected for setting image when button has touch, but you must use UIControlStateHighlighted
In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.
Hey I am writing a function that handles favorites within my app, as well as changing the image of the favorite button I want to change the image of a corresponding button in another UIScrollView DoubleScrollLeft.
The below code works HOWEVER I get the following eror when trying to favorite my first button with the tag '0' is there a reason for this? (the rest work).
-[UIScrollView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x14e3b0
Also once the images do change it pushes the title of the button off to the right, do i need to reset the frame etc.. when i change the background for state?
-(void)favButtons:(id)sender {
int i = [sender tag];
NSString *fav = [NSString stringWithFormat:#"%i", i];
if ([[Favinsults objectForKey:fav] isEqualToString:#"0"]){
[sender setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
[button setBackgroundImage:[UIImage imageNamed:#"buttonD1.png"] forState:UIControlStateNormal];
} else {
[sender setImage:[UIImage imageNamed:#"favButton0.png"] forState:UIControlStateNormal];
UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
[button setBackgroundImage:[UIImage imageNamed:#"buttonD0.png"] forState:UIControlStateNormal];
}
}
There seems to be a mis-connection where a UIScrollview is connected to the IBAction method. Check your IB connections carefully.
Your sender is somehow UIScrollview.
Change this line as, This should avoid the warning message.
[sender setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
[((UIButton *)sender) setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
You have to cast the sender to a UIButton before you can call any of the UIButton methods on it...
UIButton * newbutton = (UIButton*)sender;
[newbutton setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
etc.
[UIScrollView setBackgroundImage:forState:]
means that your sender is interpreted to be of type UIScrollView (not UIButton), and there is no method setBackgroundImage:forState: defined in UIScrollView.
Introduction:
I'm creating my UIBUTTON at runtime. And my UIButton is in a TableView. The whole function of this UIButton is: If I click on "delete OFF" or "delete ON" (normal void Method) I can delete each cell in my TableView. I took the position of my UIButton on the right side like the real delete-method.
First of all, I know that Objective-C has got a delete-method! But I cant use this method, because I have Sliders in my TableView and sliders with Objective-C delete method doesn't work together.
Because of this, I have written this Code:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
if (allowchange==YES) //<- Button to activate the delete mode
{
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(700, 23, 45, 25);
[playButton setBackgroundImage:[UIImage imageNamed:#"button_delete.png"]
forState:UIControlStateNormal];
[self.view addSubview:playButton];
[playButton addTarget:self action:#selector(deleteRow:) forControlEvents:UIControlEventTouchDown];
playButton.tag=count;
[cell.contentView addSubview:playButton];
[playButton release];}}
i am trying to add a button to the tableview and no matter what frame i specify the button is not visible. This is a custom UITableViewCell and I am adding the button in the code rather than in the xib file is so that I can click on the button and perform an action which is not didSelectRowAtIndexPath:
I have the following code in cellForRowAtIndexPath: What am I missing?
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = bframe;
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
try this
UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];