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.
Related
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];
}
Here is my code. It has started to look crowded after an hour or 2 of trying to solve this issue. I've tried setting user interaction enabled to yes, I've tried just using a button alone and not making it the subview of another view.
Right now I have filterBar > filterBarContainer > filterButton.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
// Create filter bar with specified dimensions
UIView *filterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 42, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[filterBar setUserInteractionEnabled:YES];
// Make it a subview of navigation controller
[[[self navigationController] navigationBar] addSubview:filterBar];
[filterBar setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.9f]];
[filterBar setTag:1];
// Reusable vars
// CGFloat filterBarX = filterBar.frame.origin.x;
//CGFloat filterBarY = filterBar.frame.origin.y;
CGFloat filterBarHeight = filterBar.frame.size.height;
CGFloat filterBarWidth = filterBar.frame.size.width;
// Create centre filter button with specified dimensions
UIView *filterButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 2, filterBarWidth / 2 - 30 , filterBarHeight - 10)];
[filterButtonContainer setUserInteractionEnabled:YES];
// Make it a subview of filter bar
[filterBar addSubview:filterButtonContainer];
[filterButtonContainer setBackgroundColor:[UIColor redColor]];
// Centre the button
[filterButtonContainer setCenter:[filterBar convertPoint:filterBar.center fromView:filterBar.superview]];
// Add button to filter button
UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[filterButton setUserInteractionEnabled: YES];
[filterButton setFrame:CGRectMake(0, 0,40 , 20)];
[filterButton setTitle:#"test" forState:UIControlStateNormal];
[filterButtonContainer addSubview:filterButton];
[filterButton setBackgroundColor:[UIColor yellowColor]];
// self.filterButton = filterButton;
[filterButton addTarget:self action:#selector(filterButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"dd");
filterButtonContainer.layer.borderColor = [UIColor blackColor].CGColor;
filterButtonContainer.layer.borderWidth = 1;
This is the method I'm trying to trigger.
- (void)filterButtonTapped:(UIButton *)sender
{
NSLog(#"filter button tapped");
UIActionSheet *filterOptions = [[UIActionSheet alloc] initWithTitle:#"Filter Garments"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Recommended", #"What's New", #"Price - High to Low", #"Price - Low to High", nil];
[filterOptions showInView:self.collectionView];
}
Most likely one of your parent views (superviews of your button) have a width or height that places the filter button outside of their area. The button is still displayed (if you don't define that the view should cut off subviews), but elements outside the view's area will not get any touch events. Check your superview's width and height.
You're adding filterBar as a subview of your navigationBar, but you're setting it's y-origin within navigationBar to 42. And then you're setting your filterButtonContainer's y-origin within the filterBar to 2. Assuming navigationBar's height is 44, most of the filterBar and the entirety of your filterButtonContainer and filterButton won't be within their navigationBar superview so the button wouldn't be selectable.
Edit: To make it selectable while keeping the positioning intact, I suggest adding the filter bar to self.view. For example, change:
[[[self navigationController] navigationBar] addSubview:filterBar];
to
[self.view addSubview:filterBar];
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.
I have tableView with detailDisclosure button it works fine but i want to give title to detail disclosure button so i think i need to add custom UIButton for detailDisclosure is it possible to do like i want to do this without using custom cell.
If i want to give image it works fine but how may i give title to this button like comment.
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"loginN.png" ]];
Try this..
UIButton *btn = [[[UIButton alloc]init]autoerelease];
btn .frame = CGRectMake(210.0, 0.0, 90, 25);
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"loginN.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubView: btn];
and in btnClickHandler do this to get indexPath
- (void)btnClickHandler :(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//Get your data source object from indexPath.row.
}
Why don't u set the a button as accessory view just like u try setting an image..??
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 80.0, 80.0);//Set the frame u need for the view
//Customize the button
[button setTitle:#"title" forState:UIControlStateNormal];
//And set it as accessory view
cell.accessoryView = button;
Here is the result...
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.