UIButton unresponsive when created programmatically and added to uiimageview - ios

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.

Related

How to dynamically create and delete view when user clicks on button [duplicate]

This question already has answers here:
How do I create a custom view class Programmatically? [closed]
(5 answers)
Closed 6 years ago.
Please go through this scenario.
In a UIViewController there is a UIScrollView and inside it there is UIView and a UIButton. When a user taps on the UIButton, a new UIView should be added with the same x, invialViewWidth + 30, width, height and a UIButton.
If a user clicks the second UIButton, a new 3rd UIView with a third UIButton must be created. Similarly, you have to create a n number of views.
Inside the views there will be another UIButton delete. When it is clicked, the UIView should be removed from the UIScrollView.
For example, if you want to delete the 5th UIView then you should click on the delete UIButton which is in the 5th UIView.
I have gone through lot of questions but I haven't gotten a correct answer.
Create a custom view which will contain two Buttons add and delete. You can do something like this:
-(UIView *)getNewViewWithTag:(NSInteger )tag{
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];//Set appropriate frame
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
addButton.frame = CGRectMake(0,0,50,50); //Set appropriate frame
addButton.tag = tag;
[addButton addTarget:self action:#selector(addNewView:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:addButton];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame = CGRectMake(50,0,50,50); //Set appropriate frame
deleteButton.tag = tag;
[deleteButton addTarget:self action:#selector(deleteView:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:deleteButton];
containerView.tag = tag;
return containerView;
}
-(void)addNewView:(id)button{
NSInteger tag = [button tag];
UIView *view = [self getNewViewWithTag:tag+1];
//set appropriate frame
[scrollView addSubview: view];
}
-(void)deleteView:(id)button{
NSInteger tag = [button tag];
UIView *viewToDelete = [scrollView viewWithTag:tag];
[viewToDelete removeFromSuperview];
}

Showing custom UIView when row selected

I want to know if it possible and how to implement following user action:
The user taps on a button that is shown as UIView on a tableView row. After tapping on the button, a UIView containing buttons should appear, like in the image below:
UPDATED QUESTION:
I have changed my requirements. The UIView should be shown after swipping the row from left to right.
I have included following code to my cellForRowAtIndexPath method:
//swipe left-right
UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureLeftRight:)];
[swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
[cell addGestureRecognizer:swipeLeftRight];
//end of swipe left-right
And I have added the handleGestureLeftRight method:
-(void)handleGestureLeftRight:(UIGestureRecognizer *)gec
{
{
CGPoint p = [gec locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if (gec.state == UIGestureRecognizerStateEnded) {
NSLog(#"GESTURE LEFT-->RIGHT DONE");
CGRect newSize = CGRectMake(0.0f ,0.0f, 150.f, 50.0f);
UIView *newView = [[UIView alloc] initWithFrame:newSize];
newView.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[newView addSubview:button];
[self.view addSubview:newView];
}
}
}
I hope it will work as desired, now I will need to add more buttons and to change all CGRectMake params, to fit all the buttons inside the UIView.
Now, that I have this done, I would ask you only this question:
How to setup the UIView coordinates to put it like the picture, that means just above the selected row?
1 . You can get the position of the cell on screen with:
CGRect positionOnScreen = [tableView convertRect:self.frame toView:parentViewController.view];
where self is the UITableViewCell and parentViewController is the the UIViewController containing the table. You need to do this in your custom cell. Then you can pass this value to your parent UIViewController. Add your pop-up view to your UIViewController using:
[myPopUpView setFrame: CGRectMake(myPopUpView.Frame.Origin.x , positionOnScreen.origin.y - myTableCell.Frame.Size.Heigh - myCustomCell.Frame.Size.Height, myPopUpView.Frame.Size.Width, myPopUpView.Frame.Size.Width)];
myPopUpView.Alpha = 1;
I assumed you had already created the pop-up view in your .XIB with the Alpha property equals 0.
2 . You can 'close' the pop-up by hiding it with:
myPopUpView.Alpha = 0;
You can also use addSubView: and removeFromSuperview for adding/removing the pop-up view instead of creating it in the .XIB and using the Alpha property.

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

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.

Adding Button from loop into UIScrollView

i am writing this code to create buttons from loop and adding them into UIScrollView but somehow its not working , please can someone help.
the code is
scroll = [[UIScrollView alloc]init];
[self createurdulistfordisplay];
for(int i = 1;i<[urdulist count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
[button addTarget:self
action:#selector(playurduaudio:)
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(80.0, 210.0 + (30*i), 213.0, 46.0);
[scroll addSubview:button];
}
I think you have forgotten to add scroll to view. add like his at last line
[self.view addSubView:scroll];
Here you have created scroll but not added to your view that means until you adds this scrollView to main view i.e. to self.view ,you will not get it display on screen

Resources