I created UIButtons in UITableViewCell. It is displaying properly in iphone. But when I upgrade this app for iPad UIbuttons are shifted to right side and come out of the table's boundary . Here is my code
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setFrame:CGRectMake(47.0f, 100.0f, 16, 16.0f)];
[button2 setImage:[UIImage imageNamed:#"Delete.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(delete:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button2];
return cell;
You should probably add the button to the cell's contentView, and not to the cell directly.
You might also want to place the button's frame origin relative to the contentView's frame size, + set the button's autoresizing mask so that it is placed properly according to the cell size (which can change based on device type and/or interface orientation).
Please try this one. In you code remove [cell addSubview:button2]; add write there [cell.contentView addSubview:button2]; and also for removing overlapping of cell or for removing disturb buttons frame add this for loop before all cell content view allocated.
for(UIView *view in cell.contentView.subviews)
{
[view removeFromSuperview];
}
then you can add here uibuttons uilabels , etc...
Related
I have view hierarchy as follows
MyViewController --> ScrollView(added via IB) --> ImageView (+ zooming there) ---> button added on image View(added via code)
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"map.png"]];
[scroll addSubview:imageView];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(660, 120, 40, 40);
[myButton setTitle:#"10" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(asiaPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButton setEnabled:YES];
[imageView addSubview:myButton];
later on in view controller, i defined selector as
-(void) asiaPressed:(id)sender{
NSLog(#"asiaPressed clicked");
}
But it is never going inside selector..
Pls help...i guess it is very basic error..
Thanks in advance
UIImageView has userInteractionEnabled property set to NO by default so it (and its subviews) does not receive touch events. You need to set that property to YES manually:
...
imageView.userInteractionEnabled = YES;
...
P.S. You also mention gesture recognizer in question title, however have not posted any code with it - so if it present it may also cause the problems, but the problem is most likely is what I described above
I have a UITableView and it is populated with information, detailed information and images for each cell. I also have it so when a cell is selected it brings the user to a detailed view. What I am trying to accomplish is how to tell the difference between when the user clicks a cell or the image in the cell.
I would like to display a small subview that when the user clicks a image in a cell the small sub view will stay in the original frame but give the user something else to interact with.
I have looked all around and can not seem to even find a simple explanation or somewhere to start. It is done with apps such as facebook and twitter, where in a table view you see the users profile picture and you can click this instead of the actual cell ....
Any information would be great thanks!
Thank you #Nekno I have started to attempt to try to implement this method and have a few questions if you dont mind clearing up or giving your opinion.
Here is what I have wrote so far
// Here we use the new provided setImageWithURL: method to load the web image with
//SDWebImageManager
[cell.imageView setImageWithURL:[NSURL URLWithString:[(Tweet*)[avatarsURL
objectAtIndex:indexPath.row]avatarURL]]
placeholderImage:[UIImage imageNamed:#"avatar.png"]];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setBackgroundImage:[NSURL URLWithString:[(Tweet*)[avatarsURL
objectAtIndex:indexPath.row]avatarURL]] forState:UIControlStateNormal];
//the button should be as big as the image in tableView
[button setFrame:CGRectMake(10, 10, 45, 45)];
//set the type of action for the button
[button addTarget:self action:#selector(imageSubViewAction:)
forControlEvents:UIControlEventTouchUpInside];
//more stuff
return cell }
Then I use this action that I specified in the #selector above
-(IBAction)imageSubViewAction:(id)sender{
[UIView beginAnimations:#"animateView" context:nil];
[UIView setAnimationDuration:0.5];
CGRect viewFrame = [subView frame];
viewFrame.origin.x += -300;
subView.frame = viewFrame;
subView.alpha = 1.0;
[self.view addSubview:subView];
[UIView commitAnimations];
So I know I will have to add an action to dismiss the subView but first things first.
This seems not to have any affect on the cells at all so far and I am not sure what step I would take next to make it functional.
Does the gesture method completely disable the function of didselectindexrow? because I would like to have both.
It seems as though this method with just adding a button within the cell and giving that button a action should work but I am a little lost to where to continue my journey.
Thank again I highly appreciate the help!
}
I think the easiest way would be to add a UIButton as the subview, and add your image as the background view for the UIButton, either through Interface Builder or by creating a UIImage.
Then you can hook up an event handler to the Touch Up Inside outlet that will handle the button tap.
With the UIButton, the UITableView cell selection shouldn't fire. If for some reason you're finding that the table cell is getting selected when the user touches the button, you could use a UIImageView instead of a UIButton, and then add a gesture recognizer to the UIImageView that handles the touches on the image and cancels the table cell selection.
To add the gesture recognizer to the UIImageView and cancel the touches for the UITableView:
Note: imageView is a local variable that references your UIImageView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
// by default, UITapGestureRecognizer will recognize just a tap.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
gestureRecognizer.cancelsTouchesInView = YES; // cancels table view cell selection
[imageView addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
//...
}
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
// get the imageView that was tapped
UIImageView* imageView = gestureRecognizer.view;
// do something else
}
Here is how I accomplished this for anyone who is looking for an answer you can use this at will!
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[cell addSubview:button];
[cell bringSubviewToFront:button];
button.backgroundColor = [UIColor clearColor];
//[button setBackgroundImage:[UIImage imageNamed:#"cellbutton.png"]
//forState:UIControlStateNormal]; //sets the image
[button setImage:[UIImage imageNamed:#"cellButton.png"]
forState:UIControlStateHighlighted]; //sets the highlighted image
//the button should be as big as the image in tableView
[button setFrame:CGRectMake(10, 25, 50, 50)];
//set the type of action for the button
[button addTarget:self action:#selector(detailSubView:)
forControlEvents:UIControlEventTouchUpInside];
I have a problem. I've a normal viewcontroller where I add UITextViews in a scrollview. To these UITextviews I add a dynamic number of UIButtons, to which I want to add a target. The reason I add them to the UITextViews is that adding them to the viewcontroller adding the text view's origin will make them end up outside the screen and not scroll, of course. But when I do that, the buttons trigger the action.
My question is: how do I specify the viewcontroller as target? Using self or using the var created in the appdelegate as target does not trigger it. If "two superlevels up from the textview" will work I will use that, just don't know how to specify it correctly.
My code:
UIImage *img=[UIImage imageNamed:#"phonebutton40x30.png"];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:img forState:UIControlStateNormal];
btn.tag=700+i;
btn.frame=CGRectMake(xoffs+3, yoffs+19, 50, 38);
[tvMain addSubview:btn];
Just remove colon from your code
[btn addTarget:self action:#selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];
The corrected one is
[btn addTarget:self action:#selector(phoneemail) forControlEvents:UIControlEventTouchUpInside];
If I guess, this will probably help you.
Solved it back then by adding the buttons to the scrollview instead of the textview.
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;
}
}
I'd like one of my table rows to be a button that takes up an entire row of my UITableView. I figured the best way to go about this is to instantiate a UIButton, and give it the same frame size as an instance of UITableViewCell, and add that as a subview to the cell. I'm almost there, but quite a few pixels off to not get that perfect touch to it. Is there a better approach to this, or perhapsps can my placement accuracy be fixed up to get that perfect alignment?
cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self tableviewCellWithReuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0f, 5.0f, 320.0f, 44.0f)];
[button setTitle:#"Do Stuff" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
You can fake it in your didSelectRowAtIndexPath: method.
Have the tableView cell act as the button.
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES];
[self doStuff];
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
Use setSelected: animated: to fake a button press and have it fade.
Might do the trick for you.
Class(UITableViewCell) has contentView property.
So I put basic button in contentView of UITableViewCell variable.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
・・・
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
[btn setTitle:#"OK" forState:UIControlStateNormal];
[btn setTitle:#"OK" forState:UIControlStateSelected];
[btn addTarget:self action:#selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
・・・
return cell;
}
Sorry, poor my English.
if you want it to fit perfectly, just call the cell's bounds as your CGRect. do it like this:
button.frame = cell.bounds;
That should get your button to be exactly the same as your cell. Hope that helps!
Cord
This may be beside the point, but your code may have the problem where another button is added to the cell every time the cell is re-used.
I had the same problem as you. And Ryan gave you the answer: use the cell itself as a button with the didSelectedRowAtIndexPath: method.
You can also add add a UIImage to your UITableViewCell to make the cell appear to be a button.
You can also create a view with a button and place it in the footer of the section above it. That way you don't need to worry about the cell image in the background. I've done this to place two half-width buttons side-by-side in a grouped table view.
FWIW, I put the button in my app at CGRectMake(9, 0, 302, 45)
and I think it looks perfect.
UIButton *btnView = [[UIButton alloc] init];
btnView.frame = CGRectMake(52.0f, 2.0f, 215.0f, 38.0f);
[btnView setImage:[UIImage imageNamed:#"invite.png"] forState:UIControlStateNormal];
[btnView addTarget:self action:#selector(showInviteByMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnView];
[btnView release];