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.
Related
I'm just trying to set up a simple storyboard. I have a View Controller embedded in a Navigation Controller, and it is the starting view for the app. I added a full-page Table View and a Table View Cell inside that.
Then I wanted to add two Bar Button Items to the top, but when I drag them in, that's where the trouble is. When I drag it onto the top bar, it creates a new bar at the bottom and attaches there instead. When I go to the Document Outline and manually drag it under Navigation Item in the View Controller, then it works and goes to the upper right.
But then there's no way for me to add a second Bar Button Item to the upper left-- when I drag one in, it goes to the bottom, and if I try dropping it under Navigation Item in the Document Outline along with the first one, it just ceases to exist, as though only one Bar Button Item were allowed.
What am I doing wrong? This has worked fine in the past.
Sometimes storyboards have bad days and xCode has bugs. Then code comes to rescue:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
showCalendar.frame = CGRectMake( 0, 0, 40, 30 );
[showCalendar setImage:[UIImage imageNamed:#"calendar"] forState:UIControlStateNormal];
[showCalendar addTarget:self action:#selector(METHOD CALL) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
You can repeat the process for adding another button on the left.
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
}
I wanted to put a timer in the title label of a tableViewController. To my surprise, storyboard let me drop a button the title and I got everything working, however, I need to button to differentiate between single and double taps. Single tap starts and stops the timer, and double tap would reset it back to zero. In order to make a button recognize both type of taps, I had to create a subclass of UIButton, which means that I am now adding the button programmatically rather than through storyboard. In viewDidLoad, I tried to add the button to the titleView of the navigationItem but nothing's showing when I run it in the simulator. I set the x,y coordinates like this
self.navigationItem.titleView.bounds.size.width/2
thinking it would place the button in the middle.
I'm not sure if I've messed up the coordinates or done something else wrong, but nothing's showing. Can you suggest how it might be done?
MMTimerButton *button = [MMTimerButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(self.navigationItem.titleView.bounds.size.width/2, self.navigationItem.titleView.bounds.size.height/2, 160.00, 40.0);
[self.navigationItem.titleView addSubview: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.
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.