i'm extending UITableViewController and have a uibutton added programtticly to the table footer , for some reason the uibutton fire only when touch in the top part ... as if someting is masking the rest of it and i can't understand what it is
i have the following code at viewForFooterInSection function:
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
footerView.autoresizesSubviews = YES;
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
footerView.userInteractionEnabled = YES;
goButton = [UIButton buttonWithType:UIButtonTypeCustom];
[goButton addTarget:self action:#selector(getReport)forControlEvents:UIControlEventTouchUpInside];
[goButton setTitle:#"GO!" forState:UIControlStateNormal];
[goButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
goButton.frame = CGRectMake(50, 20, 220.0, 50.0);
UIImage *bg = [UIImage imageNamed: #"but_go.png"];
[goButton setBackgroundImage:bg forState:UIControlStateNormal];
[goButton setBackgroundColor:[UIColor clearColor]];
if (self.optimizations == nil || self.optimizations.count < 1){
goButton.enabled = NO ;
}
[footerView addSubview: goButton];
return footerView;
It's because the button's frame fell out of its parent view. You need to increase your footerView's frame size so the whole part of the button contains in the footerView. You can try to set footerView background color to blue or red to see the actual frame area.
Related
I am trying to set accessory view of cell which is UIButton. But it is not shown.
Here is the code
UIButton *accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:#"plus.png"] forState:UIControlStateNormal];
[cell setAccessoryView:accessory];
Call [accessory sizeToFit]; to adjust its size according to the image.
You will need to set a frame to this accessory like this
UIButton *accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:#"plus.png"] forState:UIControlStateNormal];
//Set the accessory frame
accessory.frame = CGRectMake(0, 0, 100, 100);
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];
[accessoryViewImage release];
[accessoryView release];
just do like this.
My button is not responding to touch events, please find the code snippet as below.
I have added UIButton to UIView and set the UIView to UITableView's footerview.
Please let me know and thanks.
CGRect tblViewFrame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 300);
self.tblViewSettings = [[[UITableView alloc]initWithFrame:tblViewFrame style:UITableViewStyleGrouped]autorelease];
self.tblViewSettings.backgroundColor = [UIColor clearColor];
self.tblViewSettings.showsVerticalScrollIndicator = FALSE;
self.tblViewSettings.delegate = self;
self.tblViewSettings.dataSource = self;
self.tblViewSettings.scrollEnabled = YES;
// Set the background color
self.view.backgroundColor = [UIColor clearColor];
UIView *buttonView = [[[UIView alloc]initWithFrame:CGRectMake(10,10, 320, 60)]autorelease];
self.signoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.signoutButton.frame = CGRectMake(10,10, 300, 40);
UIImage *image = [[UIImage imageNamed:#"btn-large1.png"] stretchableImageWithLeftCapWidth:22 topCapHeight:0];
NSString *strSignOut = NSLocalizedString(#"Sign Out", #"SignOut Button");
[self.signoutButton setTitle:strSignOut forState:UIControlStateNormal];
[self.signoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.signoutButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
[self.signoutButton setBackgroundImage:image forState:UIControlStateNormal];
self.signoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
buttonView.backgroundColor = [UIColor clearColor];
[buttonView addSubview:self.signoutButton];
buttonView.userInteractionEnabled = YES;
//[self.view addSubview:buttonView];
self.tblViewSettings.tableFooterView = buttonView;
self.tblViewSettings.tableFooterView.userInteractionEnabled = YES;
[self.view addSubview:self.tblViewSettings];
-(IBAction)signOutBtnTapped:(id)sender{
}
I have looked at your code and applied it to my project. actually it works. i did not change anything from your code. i guess u must check out the frames of table view and the bottom view as everything else is working fine.
Try capturing the event for UIControlEventTouchDown instead of UIControlEventTouchUpInside in
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents: UIControlEventTouchUpInside];
Everything else seems OK.
for Adding button in Footer view Section
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
UIButton *Login_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
[Login_Btn setImage:[UIImage imageNamed:#"btn_login.png"] forState:UIControlStateNormal];
Login_Btn.frame = kiPhoneButtonFrame;
[customView addSubview:Login_Btn];
[Login_Btn addTarget:self
action:#selector(Login_Btn_Clicked:)
forControlEvents:UIControlEventTouchUpInside];
return customView;
}
And Button Click Method
-(IBAction)Login_Btn_Clicked:(id)sender
{
// Code for button CLick which you want to do.
}
I am write code for scrollview and add button on it but button not appear in scrollview.
My code
tableProductScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(75, 442, 874, 208)];
tableProductScrollView.delegate=self;
[tableProductScrollView setBackgroundColor:[UIColor clearColor]];
[tableProductScrollView setCanCancelContentTouches:YES];
tableProductScrollView .indicatorStyle = UIScrollViewIndicatorStyleBlack;
tableProductScrollView .pagingEnabled = YES;
tableProductScrollView.showsVerticalScrollIndicator = YES;
[tableProductScrollView setContentSize:CGSizeMake(1748,208)];
[tableProductScrollView setScrollEnabled:YES];
[self.view addSubview:tableProductScrollView];
CGRect frameProduct1 = CGRectMake(85, 445, 142, 150);
tableProduct1Button = [UIButton buttonWithType:UIButtonTypeCustom];
tableProduct1Button.frame = frameProduct1;
UIImage *imgProduct1= [UIImage imageNamed:#"product6-small.png"];
[tableProduct1Button setImage:imgProduct1 forState:UIControlStateNormal];
tableProduct1Button.backgroundColor = [UIColor clearColor];
tableProduct1Button.highlighted = YES;
//---add the action handler and set current class as target---
[tableProduct1Button addTarget:self
action:#selector(homeButtonAction)
forControlEvents:UIControlEventTouchUpInside];
[tableProductScrollView addSubview: tableProduct1Button];
please give me suggestion.
maybe:
UIButton *tableProduct1Button =
[UIButton
buttonWithType:UIButtonTypeCustom];
Try to make a round rect button with some color and then see if you are able to see the button or else check the frame. Or try to set an image for the highlighted state.
[tableProduct1Button setImage:image1 forState:UIControlStateHighlighted];
if ([cell.textLabel.text isEqualToString:#"Button"])
{
UIView *ButtonView = [[UIView alloc] initWithFrame:CGRectMake(100, 5, 100, 25)];
UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Button.frame = CGRectMake(90, 5, 90, 30);
[Button setTitle:#"PopOver" forState:UIControlStateNormal];
[ButtonView addSubview:Button];
[cell.contentView addSubview:ButtonView];
[Button addTarget:self action:#selector(showPickerPopupAction)forControlEvents:UIControlEventTouchUpInside];
Button.tag = indexPath.row;
}
- (void) showPickerPopupAction
{
NSLog( #"Button clicked." );
}
The Button's frame is almost completely outside the ButtonView's frame. If you set a backgroundColor on the ButtonView, you'll see that only the top-left corner of the button is in the ButtonView frame and only that corner will respond to touches.
Adjust the size of the ButtonView frame or the size and position of the Button frame so that the button is completely inside ButtonView.
Not sure why you need ButtonView in the first place. You could just add Button directly to the contentView.
Also, you should do [ButtonView release] after adding ButtonView to the cell.contentView.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ButtonCell"] autorelease];
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
backgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
cell.backgroundView = backgroundView;
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonLeft setTitle:#"Left" forState:UIControlStateNormal];
[buttonLeft setFrame: CGRectMake( 10.0f, 3.0f, 145.0f, 40.0f)];
[buttonLeft addTarget:self action:#selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRight setTitle:#"Right" forState:UIControlStateNormal];
[buttonRight setFrame: CGRectMake( 165.0f, 3.0f, 145.0f, 40.0f)];
[buttonRight addTarget:self action:#selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:buttonLeft];
[cell addSubview:buttonRight];
[backgroundView release];
-(void) addToFavorites {}
Works fine !!
I have a transparent image which ("hold_empty.png", below) which I want to turn into a button. Inside the button there will be a view with a colored background which is slightly smaller size than the actual image to hold the background color.
The reason for this is because the image has rounded corners and so I cannot simply put a background color on the image as it will appear to be bigger than the image.
The image is a transparent square with "rounded corners". The effect I am trying to create should be like layers.
Background color layer (red, green, etc)
The "hold_empty.png" picture (above)
The whole object (including bg color layer) should be clickable.
The problem I am experiencing is that only the image (and its borders) appears to be clickable, and not the whole object.
The code follows below.
// x,y,w,h
CGRect frame = CGRectMake(10, 10, 72, 72);
CGRect frame2 = CGRectMake(5, 5, 60, 60); // I know this is not filling the image, its just a test
UIView *bgColorView = [[UIView alloc] initWithFrame:frame2];
[bgColorView setFrame:frame2];
bgColorView.backgroundColor = [UIColor redColor];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"hold_empty" ofType:#"png"]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:bgColorView];
[btn setImage:image forState:UIControlStateNormal];
btn.frame = frame;
[btn addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[bgColorView release];
So, to summarize: How do I make a transparent image button with a background color clickable?
Thanks.
The clickable area is generally the frame of the button which would not be clipped by its super-views, if you would let them clip contents.
The fact that you see something on screen doesn't mean it's "visible" in the sense that you can touch it. If you let every view clip its subviews (through IB, or by setting view.clipsToBounds = YES;) then any problems in that respect will show up clearly.
(Note that any UIButton, UIImageView, etc is a view and can be set to clip its contents)
Do you need to maybe enable user interaction on the subview?
bgColorView.userInteractionEnabled = YES;
I think I may have an answer that is working, but would like your comments/suggestions.
I created an UIImageView and put my background color layer and image layer inside that and then put the UIImageView inside the btn.
The only problem is now the button does not look like it is being pressed.
When I add the "setShowsTouchWhenHighlighted" option it has what looks like a big white star in the middle of the button when the user clicks ont he button.
// x,y,w,h
CGRect frame = CGRectMake(10, 10, 72, 72);
CGRect frame2 = CGRectMake(5, 5, 62, 62); // This fits, but may not be 100% accurate
// View
UIView *bgColorView = [[UIView alloc] initWithFrame:frame2];
[bgColorView setFrame:frame2];
bgColorView.backgroundColor = [UIColor redColor];
bgColorView.userInteractionEnabled = YES;
[bgColorView setNeedsDisplayInRect:frame2];
// Image
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"hold_empty2" ofType:#"png"]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
[imgView insertSubview:bgColorView atIndex:0];
// Btn
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:frame];
[btn addSubview:imgView];
[btn setShowsTouchWhenHighlighted:YES];
[btn setUserInteractionEnabled:YES];
[btn addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[imgView release];
[bgColorView release];
I've cracked it now. One of the problems I was having was that it was not working with avatars (sometimes it would appear behind the layer, othertimes it would not show at all).
This is my solution.
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"some_avatar" ofType:#"png"]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
CGRect fullFrame = CGRectMake(25, 10, 70,72);
CGRect frame = CGRectMake(28, 13, 63,65);
UIButton *h=[UIButton buttonWithType:UIButtonTypeCustom];
[h setFrame:fullFrame];
[[h layer] setMasksToBounds:YES];
//[h setBackgroundImage:image forState:UIControlStateNormal];
//[h setImage:image forState:UIControlStateNormal];
[h setShowsTouchWhenHighlighted:YES];
[h setClipsToBounds:YES];
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
[gradientLayer setBounds:frame];
[gradientLayer setPosition:CGPointMake([h bounds].size.width/2, [h bounds].size.height/2)];
[gradientLayer setColors:[NSArray arrayWithObjects:
(id)[[UIColor darkGrayColor]CGColor],(id)[[UIColor blackColor] CGColor], nil]];
[[h layer] insertSublayer:gradientLayer atIndex:0];
[h insertSubview:imgView atIndex:1];
[h addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:h];
[imgView release];