Button in cell not responding on iPad - ipad

I have a strange problem with buttons placed in table view cells on the iPad.
Here are two screenshots for better understanding (the button is on the right side and has a white background color):
iPhone
iPad
No problem with the iPhone, but on the iPad this button is not responding.
The positioning seems to be right, as it is drawn correctly.
This is the code from my custom UITableViewCell:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setContentMode:UIViewContentModeCenter];
[rightButton setBackgroundColor:[UIColor whiteColor]];
int xPos = buttonWidth-rightButton.frame.size.width;
[rightButton setFrame:CGRectMake(xPos, 0, buttonHeight, buttonHeight)];
[self.contentView addSubview:rightButton];
//buttonWidth = 310, buttonHeight = 60
The image and target is set by another class:
[cell.rightButton setImage:[UIImage imageNamed:#"options.png"] forState:UIControlStateNormal];
[cell.rightButton addTarget:self action:#selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
I also tried this after placing all elements to the cell
[self.contentView bringSubviewToFront: rightButton];
to ensure the button is topmost.
Didn't work.
Any ideas on this?
I still could't find a solution.
What I did in the meantime:
visualized all objects placed in the cell (to display unwanted overlapping)
disabled user interaction on all other cell objects
Any other ideas?

First try this:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setContentMode:UIViewContentModeCenter];
[rightButton setBackgroundColor:[UIColor whiteColor]];
int xPos = buttonWidth-rightButton.frame.size.width;
[rightButton setFrame:CGRectMake(xPos, 0, buttonHeight, buttonHeight)];
[self.contentView addSubview:rightButton];
[self.contentView bringSubviewToFront: rightButton]
[self.contentView bringSubviewToFront: rightButton];
rightButton.userInteractionEnabled = YES;
[cell.rightButton setImage:[UIImage imageNamed:#"options.png"] forState:UIControlStateNormal];
[cell.rightButton addTarget:self action:#selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightButton setUserInteractionEnabled:YES];
Or you can also add a tap gesture to your button, it'll resolve the issue:
Instead of this line,
[cell.rightButton addTarget:self action:#selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
Write this,
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showOptions:)];
cell.rightButton addGestureRecognizer:tap];
[cell.rightButton setUserInteractionEnabled:YES];

Okay, I found something:
For the iPad the button only responds if it is placed in between the first 320 pixels horizontally.
Here is what the debugger says when touching the button (placed far on the right side):
(lldb) po touches
(NSSet *) $1 = 0x08249cd0 {(
<UITouch: 0x82c63d0> phase: Began tap count: 1 window: <UIWindow: 0x8266ca0;
frame = (0 0; 768 1024); opaque = NO; autoresize = RM+BM;
layer = <UIWindowLayer: 0x8266d60>> view: <SBMultiCell: 0x82cb8a0; baseClass = UITableViewCell;
frame = (0 0; 744 93); autoresize = W; layer = <CALayer: 0x82cb830>>
location in window: {713, 123} previous location in window: {713, 123}
location in view: {701, 44} previous location in view: {701, 44}
)}
(lldb) po rightButton
(UIButton *) $2 = 0x082cd500 <UIButton: 0x82cd500;
frame = (658 0; 86 86); opaque = NO; layer = <CALayer: 0x82cd5e0>>
The touch location is exactly in between the button dimensions and also everything is drawn correctly. And as I wrote before, there are no overlapping elements or the like...

Related

UIButton contentVerticalAlignment doesn't center button image

I created a UIButton programmatically and set its image, but the image just won't center vertically, it's always rendered at the bottom of the button.
Here is my code:
CGFloat logoutButtonSize = self.profileItem.detailView.frame.size.height;
CGFloat logoutButtonX = self.profileItem.detailView.frame.size.width - logoutButtonSize - 8;
CGRect logoutButtonFrame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
UIButton *logoutButton = [[UIButton alloc] initWithFrame:logoutButtonFrame];
[logoutButton setImage:[UIImage imageNamed:#"logout.png"] forState:UIControlStateNormal];
logoutButton.imageView.contentMode = UIViewContentModeCenter;
logoutButton.contentMode = UIViewContentModeCenter;
[logoutButton addTarget:self
action:#selector(confirmLogout)
forControlEvents:UIControlEventTouchUpInside];
logoutButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
logoutButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
logoutButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
After setting a distinct background color for the imageView of the button I can see that it's still aligned to the bottom:
(blue = button, red = image view)
Why is it not working?
Based on your posted image, it looks like the Button itself is being clipped at the bottom, as opposed to the image not being centered vertically.
Your code:
CGRect logoutButtonFrame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
says the Button (blue rectangle) should be square. Yet in the image you posted, the blue rectangle is 120 x 88.
The bottom portion of your button is not being displayed, so it looks like your image is not centered.
By the way, you shouldn't need much of the code you are using:
// define the frame size
CGFloat logoutButtonSize = self.profileItem.detailView.frame.size.height;
CGFloat logoutButtonX = self.profileItem.detailView.frame.size.width - logoutButtonSize - 8;
CGRect logoutButtonFrame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
// instantiate the UIButton
UIButton *logoutButton = [[UIButton alloc] initWithFrame:logoutButtonFrame];
// set the image
[logoutButton setImage:[UIImage imageNamed:#"logout.png"] forState:UIControlStateNormal];
// add the button action target
[logoutButton addTarget:self
action:#selector(confirmLogout)
forControlEvents:UIControlEventTouchUpInside];
// set the resizing mask properties
logoutButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
// shouldn't need any of the following
//logoutButton.imageView.contentMode = UIViewContentModeCenter;
//logoutButton.contentMode = UIViewContentModeCenter;
//logoutButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//logoutButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
Point 1
AFAIK, UIControlContentVerticalAlignmentCenter & contentHorizontalAlignment is for text alignment and not for image....
Point 2
UIButton *logoutButton = [[UIButton alloc] initWithFrame:logoutButtonFrame]; will create basic button (like < iOS 6) opposed to Custom button. Below is how you should have button programmatically.
UIButton * logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
[logoutButton setBackgroundImage:[UIImage imageNamed:#"logout.png"] forControlEvents:UIControlEventTouchUpInside];
[logoutButton addTarget:self action:#selector(confirmLogout) forControlEvents:UIControlEventTouchUpInside];
[logoutButton setTitle:#"" forState:UIControlStateNormal];
logoutButton.frame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
[self.view addSubview: logoutButton];
Now for image to adjust,
continueButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
You are done...
And last
Your code is not working because your UIButton is not Custom button.

iOS UIButton can't click in UIScrollView

I had search some post before. But I can't try out the correct result.
I generate more button and add the view in the UIScrollView.
I had set autolayout in storybaord about the view and scrollview.
But the Button click are not listener. I don't know why.
I had set
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
But the button still can't click.
My code is below:
_sclVW.delegate = self;
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
int btnWidth = 80,btnHigh=50;
for( int i = 0 ; i < 50 ; i++ )
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(btnSendTag:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
button.tag = i;
button.titleLabel.numberOfLines = 0;
button.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.userInteractionEnabled = YES;
[button setEnabled:YES];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor darkGrayColor]];
if( i < 3)
{
button.frame = CGRectMake( i *(btnWidth + 20), 0 , 80,50 );
}
else
{
button.frame = CGRectMake( (i%3) *(btnWidth + 20), (i/3) *(btnHigh + 20) , 80,50 );
}
[_contentVW addSubview:button];
}
}
-(void) btnSendTag:(UIButton *)sender
{
NSLog(#"btn click:%li",(long)sender.tag);
}
Have anyone know how to resolve the problem?
thank you very much.
I had post my simple test project in github(this code).
I found your issue from sample project. Issue here is height and width of contentVW in Storyboard is 0. So that subview (buttons) of this view can not be interact with user.
Look at the picture below:
Here you can see that subview of UIScrollView has value 0 for width and height.
So change width and height of view same like scrollview and than you can able to tap on these buttons.

Adding a close button to the modal view that is partially off the view

I am trying to add a "X" button to the top of the modal view so if the user presses that it closes the modal view. I looked at the solution proposed here how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?
But I have two follow up questions here as i am fairly new to ios programming
The solution proposed in the above page does not seem to work for me. I tried it with a normal button first in the viewDidLoad of the modal VC and I am seeing it appear only within the bounds of modal view and not outside as I want.
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonView.backgroundColor = [UIColor brownColor];
CGRect buttonFrame = CGRectMake( 0, 0, 100, 50 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: #"Close" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[button addTarget:self
action:#selector(closeButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
CGRect parentView = self.view.superview.frame;
CGRect currentView = self.view.frame;
CGRect buttonViewFrame = buttonView.frame;
buttonViewFrame.origin = CGPointMake(parentView.origin.x - buttonViewFrame.size.width/2.0f, parentView.origin.y - buttonViewFrame.size.height/2.0f);
[buttonView setFrame:buttonViewFrame];
[self.view addSubview:buttonView];
what I am seeing is
How do I draw the "X" button do I use CGRect to draw it our or instead of adding a button in the above example should I just add a image with the "X" in it as the subview?
thanks in advance for the help
do like this, hope helps u :)
//adding button to your view
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 300, 350)];
aView.backgroundColor = [UIColor blueColor];
aView.tag = 100;
[self.view addSubview:aView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitle:#"CLOSE" forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = CGRectMake(aView.bounds.origin.x, aView.bounds.origin.y, 80, 30);// adjust with respect to bounds
aButton.backgroundColor = [UIColor redColor];
[aView addSubview:aButton];
[aView release]; //i am doing without ARC
The other option is to create a larger view and put your UITableView within this, along with your close button. The close button can then be at position (0,0) and the UITableView can start at (25,25).
This prevents complications - I have seen clipped views fail to recognise the touch outside of the main view, so your 50x50 button only has a touch area of 25x25.

UIButtons receiving touch events when added directly to scrollview but not indirectly

I have 10 UIButtons stacked side to side in my app , and the total dimensions of the buttons are 640x96.
If I add these buttons directly to the UIScrollView, they register touch events and are scrollable.
But if I try to add them to a plain UIView, then add that UIView to the scrollview, they dont work.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *fview = [[UIView alloc] init];
fview.frame = CGRectMake(0, 0, 640, 96);
fview.backgroundColor = [UIColor blackColor];
[fview setUserInteractionEnabled:YES];
[_sv setUserInteractionEnabled:YES];
for (int i=0; i<10; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(64*i, 0, 64, 96);
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[fview addSubview:button];
}
[_sv addSubview:fview];
_sv.contentSize = CGSizeMake(640, 96);
}
-(void)buttonClicked:(id)sender {
NSLog(#"Clicked!");
}
While I was writing the question I fixed the problem :D
All I had to do was to set the frame of the UIView before I set the scrollview's contentsize.
In my case:
[_sv addSubview:fview];
fview.frame = CGRectMake(0, 0, 640, 96); // Added line
_sv.contentSize = CGSizeMake(640, 96);
Hope this helps!

UIButton unresponsive when created programmatically and added to uiimageview

I have the following code in my viewDidLoad method. This is a uiviewcontroller with scrollview and page controller.
I have the following code which creates the views from an array puts a button on the 3rd page:
for (int i = 0; i < imageNames.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
UIImageView *myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
myImageView.image=[UIImage imageNamed:[imageNames objectAtIndex:i]];
[subview addSubview: myImageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button addTarget:self
action:#selector(doSomething:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
if(i == 1){
[myImageView addSubview:button];
}
[self.scrollView addSubview:subview];
}
I have an ibaction that will dismiss this view as it modal view:
-(IBAction)doSomething:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
The problem is that this button is unresponsive. It doesn't even highlight when touch let alone call the doSomething method.
What am I doing wrong?
Note sure if this is the issue but probably.
From the UIImageView docs:
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
Posted for the benefit of future searchers...
If enabling the userInteractionEnabled property of the UIImageView hadn't worked, another thing to look at is whether the UIButton's view is outside the frame of its superview.
When an object's view is outside the frame of its superview, it will appear to the user but it will not be able to receive touch events.

Resources