I have added uibutton in a cell in uitable view. I want this button to be pressed and interat with user. I added this line of code in cell implementation class.
readMore = [[UIButton alloc]initWithFrame: CGRectMake(180, 50, 67, 25)];
[readMore addTarget:self action:#selector(readMoreClic:) forControlEvents:UIControlEventTouchUpInside];
[readMore setImage:[UIImage imageNamed:#"readmore.png"] forState:UIControlStateNormal];
[self addSubview:readMore];
the problem is that it is not clicked (it is always in highlight state after selecting the cell). I tried :-
1- [self setSelectionStyle:UITableViewCellSelectionStyleNone];
2- cell.userInteractionEnabled = NO
but I still get cells selected and highlighted with blue color and button not clicked. even after i've chosen
[self setSelectionStyle:UITableViewCellSelectionStyleGray]
some one help plz
thx
[note i have a textView in the same cell and it allows interaction (select and copy) ]
[i am using xcode 4 with iOS 4.3]
try a different control event?:
[readMore addTarget:self action:#selector(readMoreClic:) forControlEvents:UIControlEventTouchDown];
well, i don't quite understand why your button don't respond, because i've came across similar problems but i've used different solutions. here's what i've done:
CGRect reuseableRect = CGRectMake(20, 290, 260, 37);
self.actionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.actionButton.frame = reuseableRect;
[self.actionButton setTitle:#"Initialize" forState:UIControlStateNormal];
[self.actionButton addTarget:nil action:#selector(performAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.actionButton];
Actually, i did this :
self.actionButton = [[UIButton alloc] initWithFrame:reuseableRect];
but it didn't work. so i changed it into
self.actionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
the resueableRect is just a CGRect that I've used all through the view initialization.
as for the three lines of your previous solution, the first one:
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
it's doing some configurations to the tableView and its cells, so it's not going to affect the button
the second one:
cell.userInteractionEnabled = NO;
it's not configuring the button as well, what's more, since the cell is the super view of the button, this line prevents any user interactions.
the third one is not doing anything to the button as well, so i recommend that you delete all of them if you were thinking about configuring the button
You don't need to do anything with selection style or userInteractionEnabled. Make sure that readMoreClic: looks like:
-(void)readMoreClic:(id)sender
{
}
Related
This is my code
UIImageView *bgDetails = [[UIImageView alloc] initWithFrame: CGRectMake(0, 20, 320, 47)];
bgDetails.image = [UIImage imageNamed:#"bgDetail"];
UIButton *btnLike = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLike setBackgroundImage:[UIImage imageNamed:#"38_38"] forState:UIControlStateNormal];
btnLike.frame = CGRectMake(278, 4.5f, 37, 37);
[btnLike addTarget:self action:#selector(doLike:) forControlEvents:UIControlEventTouchUpInside];
[bgDetails addSubview:btnLike];
[cell.contentView addSubview:bgDetails];
But btnLike does not call the doLike method. Please help me! Thanks!
Two possible issues here. The first one is this: I'm not sure whether it's just a typo, but you write
action:#selector(doLike:)
but then later you complain about the selector doLike which does not have the trailing colon (i. e. it takes no arguments). If this is the problem, you can fix it by changing either one, so that the two selector names match (they both need to be doLike: or doLike, consistently).
The other error I see is that you're adding the button as a subview of an image view. UIImageView has user interaction turned off by default, so in order it to react to user touches, you have to set its userInteractionEnabled property to enabled:
bgDetails.userInteractionEnabled = YES;
I want to add a button in a UITableViewCell. This is my code: `
if (indexPath.row==2) {
UIButton *scanQRCodeButton = [[UIButton alloc]init];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
}`
Now, when I run the app, I see only a blank row ! Any ideas ?
While it's natural to put it in the contentView of the cell, I'm fairly certain that is not the problem (actually, in the past, I've never had subviews displayed correctly in the contentView, so I've always used the cell).
Anyway, the problem involves the first three lines of when you start creating your button. The first two lines are fine, but the code stops working with:
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonWithType: is actually a convenience method to create a button (it's like a compact alloc-init). Therefore, it actually "nullifies" your past two lines (you basically created the button twice). You can only use either init or buttonWithType: for the same button, but not both.
UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
This will work (note that you can use cell.contentView if you wanted). In case you're not using Automatic Reference Counting (ARC), I would like to mention that you don't have to do anything in term of memory management, because buttonWithType: returns an autoreleased button.
UIButton *deletebtn=[[UIButton alloc]init];
deletebtn.frame=CGRectMake(50, 10, 20, 20);
deletebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn addTarget:self action:#selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deletebtn];
or
// Download class and import in your project UIButton+EventBlocks
UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setFrame:CGRectMake(170,5, 25, 25)];
deletebtn.tag=indexPath.row;
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn setOnTouchUpInside:^(id sender, UIEvent *event) {
//Your action here
}];
[cell addSubview:deletebtn];
You want to add any custom UI elements to the cell's contentView.
So, instead of [cell addSubview:scanQRCodeButton];
do [cell.contentView addSubview:scanQRCodeButton];
Try adding [cell.contentView addSubview:scanQRCodeButton]; or if you want the button to the left side look at my question at the answer, to move the textLabel to the side. If you want the button to the right then just set it as your accesoryView like this cell.accesoryView = scanQRCodeButton;.
Method setTitle does not work in my code, so I have set by using
[UIButton.titleLabel setText:#""]
instead of using setTitle method.
Please try again with following code:
[scanQRCodeButton.titleLabel setText:#"Hello"];
Then it would work well.
In a tableviewcell I have a UISlider. If I move the slider knob, go back to my previous view, and then return back to the table view, the knob on the slider returned back to zero but I see a "ghosting" of the knob where I had previously moved the slider too.
I clear the context view on the slider object in cellForRowAtIndexPath: and reload the table in viewDidAppear.
Anyone know how to fix this? It's quite annoying. I put the slider code down below if that helps at all.
// Setup slider
CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
UISlider *slider = [[UISlider alloc] initWithFrame:sliderFrame];
slider.clearsContextBeforeDrawing = YES;
[slider addTarget:self action:#selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
[slider addTarget:self action:#selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:slider];
[slider release];
I appreciate it! Thanks!
It is most likely that you are creating a new UISlider over the old UISlider which gives the ghosting effect.
Two possible solutions
Tag the UISlider when you add it to a cell.
Subclass UITableViewCell and add a UISlider to it's content view and keep a reference to it with an ivar.
To do 1 simply tag the UISlider when you add it to the contentView. Then when you get a cell again you try getting the view back first or you create it a fresh.
const int sliderViewTag = 99;
UISlider *slider = [cell.contentView viewWithTag:sliderViewTag];
if (!slider) {
CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
slider = [[UISlider alloc] initWithFrame:sliderFrame];
slider.clearsContextBeforeDrawing = YES;
[slider addTarget:self action:#selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
[slider addTarget:self action:#selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:slider];
[slider release]; slider = nil;
}
Although 2 is a little more involved it is my preferred method but I am sure there are some great examples of how to do it. There is some great docs by Apple so check them out Table View Programming Guide specifically look at the section A Closer Look at Table-View Cells
Apple has many big colored buttons, that are not standard. One of them is a delete contact button in address book. Another is Start/Stop button in Timer (Clock application) or End Call button in Phone. They are different, but all have similar appearance.
Question is simple. Is there a way to create these kind of buttons without using background images/screenshots or recreating them from scratch?
Thanks.
You could try Opacity. The new release allows you to draw vector images and generate UIKit-friendly Objective-C code that recreates the drawing.
Well, I've tried many ways to do it, but the most simple was to make a UIButton with a custom style.
If you need a "delete" button in the footer of the table - like in contact or event, here is the code:
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIButton *newDeleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newDeleteButton.frame = CGRectMake(10, 10, 300, 43);
UIImage *buttonBackground = [UIImage imageNamed:#"ButtonDelete.png"];
[newDeleteButton setImage:buttonBackground forState:UIControlStateNormal];
[newDeleteButton addTarget:self action:#selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILabel *deleteTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 39)];
deleteTitle.text = #"Delete";
deleteTitle.font = [UIFont boldSystemFontOfSize:20];
deleteTitle.textColor = [UIColor whiteColor];
deleteTitle.textAlignment = UITextAlignmentCenter;
deleteTitle.backgroundColor = [UIColor clearColor];
[footerView addSubview:newDeleteButton];
[footerView addSubview:deleteTitle];
formTableView.tableFooterView = footerView;
First, you create the view for footer.
Then you make a button with the correct background — just make a screenshot of any button you need and delete letters from it.
Than you create a caption for your button and place it over the button.
The last step is to put the button and caption in the footer view, and put the footer view into the table.
I thought to be clever and just put an transparent UIButton over an UIImageView with the exact frame size, so that I can wire it up easily with any event I like, for example TouchUpInside, and make it call a action method of an view controller when the user touches it. Well, it works until alpha is below 0.1f. If I do 0.01f, it will not work. So to get it work, when looking a long time on the screen, you'll see that 0.1f of alpha shining through. And that's totally disgusting ;)
It seems like iPhone OS trys to be clever and won't catch events on the button if it's visually not there. Any idea how to solve that?
Sure I could make a subclass of UIImageView and implement touchesBegan:... etc., but it doesn't feel really elegant. I mean...when I want to hyperlink an image on the web, I would never want create my own HTML element for that image, just to wire it up to an url when it's clicked. That just doesn't make a lot of sense to me.
You should be able to set the button's 'Type' to Custom in Interface Builder, and it will not display any text or graphical elements over the UIImageView. This way, you don't need to adjust the alpha. If the view is built from code, use:
button = [UIButton buttonWithType:UIButtonTypeCustom];
In addition to UIButtonTypeCustom, I set the button text colors to the following:
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
The best way of doing this is:
myButton.hidden = YES;
You can't use the buttonType property because it is a read only property. Only way to use it is when creating your button dynamically.
i also beleive you can assign an image to a button.
The image can take up the entire frame and can also have no other artifacts of the buttone if you set it up right.
check out the Property
UIButtonInstance.currentImage
That way you are not hogging your resources with elements that are essentially already there.
You can hide a button (or any object) and keep it active by adding a mask to its layer. The button will be invisible but will still catch events.
let layer = CAShapeLayer()
layer.frame = .zero
myButton.layer.mask = layer
Jasons answer above is nearly correct, but setting the button type is not possible. So to programmatically create an empty button, use this code:
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame=frame;
[self.view addSubview:myButton];
This is what i did but with using a subclass of UIButton which i later found out should not be subclassed as per the net. My subclass was called Points
Points *mypoint=[Points buttonWithType:UIButtonTypeCustom];
then if you have an image you want to add to the button :
[mypoint setImage:imageNamed:#"myimage"] forstate: UIControlStateNormal];
if you dont add this image then the button will be invisible to the user but should respond to touch. Thats how i created a hotspot on my imageView inorder to have it respond to user interaction.
It's the only way I found...
[yourButton setImage:[UIImage imageNamed:#"AnEmptyButtonWithTheSameSize.png"] forState:UIControlStateNormal];
Take care of the image. It must be .png
Custom UIButtons respond to user interactions unless their alpha is set to 0. Put a custom UIButton on top of your imageView and connect to buttonPressed action. I have also set an additional highlighted image for my UIView, now it really behaves like a UIButton. First I have defined a duration for the UIView for staying highlighted:
#define HIGHLIGHTED_DURATION 0.1
Then set the image view highlighted if the button is pressed and start a timer to keep it highlighted for that duration. Do not forget to set the highlighted image for your imageview.
- (IBAction)buttonPressed:(id)sender {
[_yourImageView setHighlighted:YES];
_timer = [NSTimer scheduledTimerWithTimeInterval:HIGHLIGHTED_DURATION
target:self
selector:#selector(removeHighlighted)
userInfo:nil
repeats:NO];
}
And simply undo highlighting when the timer finishes:
-(void) removeHighlighted{
_yourImageView.highlighted = NO;
}
lazyImageView = [[UIImageView alloc] init];
button=[[UIButton alloc]init];
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethodForVideo:)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"BackTransparent.png"] forState:UIControlStateHighlighted];
[button setTitle:#"" forState:UIControlStateNormal];
lazyImageView.frame=CGRectMake(x, y, w, h);
button.frame=CGRectMake(x, y, w, h);
Set frame of button and Image both have same frame .I use this code and working fine.Also set button background image forState:UIControlStateHighlighted so when you click on that when you see the click effect.
I managed to do it using the following code.
If the iPad is landscape the button will be located in the top right of the screen.
UIButton *adminButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
adminButton.frame = CGRectMake(974.0f, 0.0f, 50.0f, 50.0f);
[adminButton setBackgroundColor:[UIColor clearColor]];
[adminButton setTag:1];
[adminButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:adminButton];
Pressing the 'adminButton' will run the following function:
- (void)buttonPressed:(id)sender {
int buttonId = ((UIButton *)sender).tag;
switch(buttonId) {
case 1:
NSLog (#"Admin button was pressed");
break;
case 2:
//if there was a button with Tag 2 this will be called
break;
default:
NSLog(#"Key pressed: %i", buttonId);
break;
}
}