UIView/UIButton coordinates flipped - ipad

I have a coreplot project. The class of the view in viewcontroller has been changed to CPTGraphHostingView.
After creating the graph (this works fine),
I am adding a button as a subview , this launches a popover
the button shown is inverted. I have the same control in different view controllers and it works fine. The Popover also appears fine.
Here is the code that creates and adds the UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(monthSelection)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Change Month" forState:UIControlStateNormal];
button.frame = CGRectMake(330, 650, 111, 40);
[self.view addSubview:button];
[here is a screenshot of whats happening. UIButton is suppose to show somewhere below the popover. What am i missing.

Do not add subviews to the graph hosting view. Instead, make other views siblings of the hosting view, i.e., add them as subviews of the same superview.

Related

Custom UIButton No Longer Works After Changing UIView to UIViewController

This code worked in a UIView container, but after changing that container to a UIViewController, the code no longer works:
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
// set button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(60, 30, 220, 220);
[button setImage:self.obj.img forState:UIControlStateNormal];
[button addTarget:self action:#selector(selected:) forControlEvents:UIControlEventTouchUpInside];
[button setUserInteractionEnabled:YES];
[self.view addSubview:button];
}
-(void) selected:(UIButton *) button // also tried (id) sender
{
NSLog(#“Button Selected“);
}
These containers are created programmatically (dynamically), from web services, so I cannot use Storyboard. The reason for changing to a UIViewController is that I need a segue to another screen. Also, these containers are placed in a UIScrollView.
Everything else is working properly, except the selector.
Setting the userInteraction wasn't necessary earlier, but I've gone ahead and tried that in different configurations. I've added this to the scrollview, the viewController, and the button, just for good measure.
I even tried some online tutorials for programmatically creating UIButtons in UIViewControllers and was able to do that just fine. Not sure what the issue is here. Thanks.

Button is not touchable

I've set up views and buttons programatically, and i'm adding/removing views a lot. What i'm doing is also adding buttons on certain views, and i'm having trouble with a very specific one.
The structure :
I have a main view, on top of which i added another view (same size, which is the whole size of the tablet). This view contains buttons, images, etc. This is all working fine there.
One of these buttons is a "menu" which makes a simple menu appear so you can navigate to other views. This menu is another view (a third one) that adds itself on top of everything. Works fine here too.
Important note : that menu is very small compared to the whole frame, but the menu view still takes the whole space, its just 70% transparent.
The problem :
What is not working, is my way to remove that menu when, for example, i decide to click in the transparent space that is around my visible menu, it just closes it.
To do that, i decided to add a transparent background button (that takes the whole frame and is just behind my visible menu, but still in my menu view frame).
So basically : i click on a button and it closes views and just does stuff. Here is the code i'm using :
(...)
UIButton *backgroundCloser = [[UIButton alloc]initWithFrame:_menuViewController.view.frame];
[backgroundCloser addTarget:self action:#selector(closeMenuView) forControlEvents:UIControlEventTouchUpInside];
backgroundCloser.backgroundColor = [UIColor orangeColor];
backgroundCloser.alpha = 0.2;
[self.view addSubview:backgroundCloser];
[self.view bringSubviewToFront:backgroundCloser];
[self.view addSubview:_menuViewController.view];
[self.view bringSubviewToFront:_menuViewController.view];
- (void)closeMenuView{
stuff }
Right now my button is slightly orange and transparent so i'm sure its there. And i can see it. But my " closemenuview " NEVER gets called on tap. or at all.
I've tried putting it as IBAction, but in vain.
Any clue of what's happening to my baby?
If you are adding buttons on various view try writing
YOUR_View.userInteractionEnabled = YES;
and setUp Button as below
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
Fixed it.
It's always the same, i look for everything for about an hour, and as soon as i post on stack, i just find the answer by myself.
I was simply adding it to the wrong view (self.view) instead of _menuviewcontroller.view.

Can we add button on scroll view

I want to select multiple images from gallery and want to upload but before uploading I want to add a cancel button which help's user in deleting the image/image's after selection if user wants to. So how can I add a cancel button on image view ? I've tried the below code for adding button but when I select the multiple image's or single image then this button is not visible.
UIImage *imgview1=(UIImage*)[UIImage imageNamed:#"overlay.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setImage:imgview1 forState:UIControlStateNormal];
button.frame = CGRectMake(25, 25, 60, 60);
[_scrollView addSubview:button];
I want to make the User Interface of the page like this image!
Unfortunately I can't comment yet, but could you provide a bit more clarification?
My understanding from the question is that you would like to have a scroll view with many buttons in it (the buttons will be selectable images), and on those buttons you would like to add an (x) button like in the image that will respond to taps.
If that is your question then I think the simplest answer is to add another button as a subview of the image when it is selected. When that subview button is tapped you can respond in a method like below:
(void)xTapped:(UIButton *)xButton
{
UIButton *myImage = xButton.superview;
// then unselect the superview here
}

programmat button and storyboard button

I have one button and one search field in navigation bar and i added a new button by code from one class for all view.
The problem is: with the button view the other button and the search field added with storyboard are not more touchable and not working.
UIImage * buttonImage = [UIImage imageNamed:#"chat-notify.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goChat)
forControlEvents:UIControlEventTouchDown];
//[button setTitle:#"1" forState:UIControlStateNormal];
button.frame = CGRectMake(280.0, 25.0, 30.0, 30.0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:button];
and then in the controller:
notificationViewController* notification = [[notificationViewController alloc]init];
//[notification methodA];
[notification makeButtonNotification];
[self.view addSubview:notification.view];
Does anyone know where is my mistake?
Thanks
Why are you adding the button surely having it there and changing it's hidden state would be easier then you can keep it all in the storyboard
UIControlEventTouchUpInside
is what you should use not "UIControlEventTouchDown"
TouchUpInside is usually the event where you trigger the action from a button. It's when the user is lifting up from the button after pressing it. You don't want to trigger the action on Touch Down as the use may move his finger off the button to cancel the press, which is standard and expected behaviour on iOS
UPDATE: Base on your comment to this answer perhaps you need to add the button to a specific point in your view heir achy. As in your storyboard views will be added as they are listed in that stroyboard starting from the top down, with the one on the bottom being the top view.
Try obviously making sure that views that you want to receive touches are not directly on top of each and that they do not overlap. Also try adding the programatically generated button above or below a specific view, rather than just making it the top subview in your main view which by the sounds of it isn't what you want.
As you're adding a button like this
[self.view addSubview:button];
it may be better to do it more like this
[self.view insertSubview:button aboveSubview:someOtherView]
To do this you'll probably need to hook up your "someOtherView" to an IBOutlet so you can reference it in your code. As it looks like you're most likely sticking your button over everything else, when you need to be more precise.

how to add the buttons to the popover view in ipad app

i want to add some text fields and the buttons to the popover view ...
how to add those and i hav to read the text added into text field by user later ...
....should i need seperate view controller class to control popover view or can i control with existed one which calls popover view....
any help Appreciated..
Create a UIViewController using IB or programatically. Add needed buttons, textfields and maybe some logic there and use this viewController for initializing an UIPopoverController (as Gendolkari suggested).
The UIPopoverView is created by passing in a ViewController which controls the content of your popover. Check out the initWithContentViewController:, this allows you to pass your own ViewController. That ViewController would have a view with the text fields and button that you want.
Try using this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.tableView addSubview:button];

Resources