I have a UIViewController called MyViewController. MyViewController's view has an UIButton called MyFirstButton as a subview. Then MyViewController's view adds another subview called MySubView. MySubView is placed directly over MyFirstButton, so MyFirstButton isn't seen. MySubView in its turn has an UIButton called MySecondButton as a subview.
The issue is - if MySecondButton's frame overlaps MyFirstButton's frame, then MySecondButton doesn't respond.
I tried to fix it by setting userInteractionEnabled to NO for MyViewController's view and YES for MySubView, but it didn't help. The button still not responding. How do i enable a button which is located over another?
You cant use the action for that particular MyFirstButton which is under MySecondButton... I dnt think this can be done .. what you can actually do is hide MySecondButton on its click to reveal the MYFirstButton and then on the MYFirstButton click have its action performed and reveal the MYSecondButton again on top of the first one :) likewise play hidden with the subviews 2...
try this,
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *firstButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[firstButton setTitle:#"first" forState:UIControlStateNormal];
firstButton.frame=CGRectMake(40, 70, 80, 80);
[firstButton addTarget:self action:#selector(firstButtonClicked) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
[firstButton addSubview:view];
UIButton *secondButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[secondButton setTitle:#"second" forState:UIControlStateNormal];
secondButton.frame=CGRectMake(0, 0, 80, 80);
[secondButton addTarget:self action:#selector(secondButtonClicked) forControlEvents: UIControlEventTouchUpInside];
[view addSubview:secondButton];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)firstButtonClicked {
NSLog(#"first");
}
-(void)secondButtonClicked {
NSLog(#"second");
}
Related
I have a UIButton and I am adding a subview to the UIButton which is a UILabel. When I tap on the UILabel the action of the button doesn't get triggered. Is there an easy fix so that the touch event gets passed to the UIButton when the UILabel is tapped?
I am thinking of adding a gesture recognizer to the UILabel which then triggers the action of the UIButton, not sure if there's a much more simpler way.
You can probably just disable user interaction to your label.
_yourLabel.userInteractionEnabled = NO;
try this,
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *firstButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[firstButton setTitle:#"first" forState:UIControlStateNormal];
firstButton.frame=CGRectMake(40, 70, 80, 80);
[firstButton addTarget:self action:#selector(firstButtonClicked) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
label.text=#"hai";
label.backgroundColor=[UIColor redColor];
[firstButton addSubview:label];
}
-(void)firstButtonClicked {
NSLog(#"first");
}
I have view hierarchy as follows
MyViewController --> ScrollView(added via IB) --> ImageView (+ zooming there) ---> button added on image View(added via code)
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"map.png"]];
[scroll addSubview:imageView];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(660, 120, 40, 40);
[myButton setTitle:#"10" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(asiaPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButton setEnabled:YES];
[imageView addSubview:myButton];
later on in view controller, i defined selector as
-(void) asiaPressed:(id)sender{
NSLog(#"asiaPressed clicked");
}
But it is never going inside selector..
Pls help...i guess it is very basic error..
Thanks in advance
UIImageView has userInteractionEnabled property set to NO by default so it (and its subviews) does not receive touch events. You need to set that property to YES manually:
...
imageView.userInteractionEnabled = YES;
...
P.S. You also mention gesture recognizer in question title, however have not posted any code with it - so if it present it may also cause the problems, but the problem is most likely is what I described above
I want to show a subview that will contain list of menu when a UIbutton is pressed. Should I use vertical SegmentControl?
Thanks for any help in advance
You don't have to use vertical TabbarViewController.
First of all, go to the interface builder and drag a Button, connect the Button to an action method you declare in the .m file
let's said that the method is
- (IBAction) btnHandler :(id)sender {
}
then declare a new view and add it to your main view
so the btnhandler method will look like this
- (IBAction) btnHandler :(id)sender {
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 300)];
//here add what ever you want to the new created view using the
//[subview addSubview:(your Componant)];
[self.view addSubview:subview];
}
please forgive me if i made a mistake, this is not tested code. i write it now in the browser.
If its an iPad application, maybe consider calling a UIPopOverController
You could use an Action Sheet if you're aiming for a native iOS look and feel.
Agree with the previous answer that a pop over is a great option if this is an iPad app.
You could go full custom of course with your own viewController which you can animate in from the top or bottom depending on your need.
Option 1:
Design the view in your nib.
Option 2:
Have a separate view controller
You can use addSubView method to add the view when you press the UIButton
Try below code that will help you.
- (void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
[super viewDidLoad];
}
-(IBAction)buttonPressed:(id)sender
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 200, 300, 100)];
view.backgroundColor = [UIColor clearColor];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
}
I'm trying to use dynamic buttons created via code (no IB) in my project and they appear where and how I want them but they don't fire their actions.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(475, 302, 49, 58);
[button1 setTitle:#"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:#selector(goToFirstTrailer)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[myImageView addSubview:button1];
-(void)goToFirstTrailer {
[self startAnimator:#"OutsideToTrailer1_" forNumFrames:60];
}
The imageView this is placed on has buttons and User Interaction Enabled On.
Any light you can shed would be much appreciated.
I think you have the wrong signature of the action method change that line to
-(void) goToFirstTrailer:(id)sender {
and where you set the action to
[button1 addTarget:self action:#selector(goToFirstTrailer:) forControlEvents:....
Important is the colon after the message name because I changed the action method signature to include the sender.
Edit I wrote a small sample project with just an imageView in the MainWindow.xib and created a button programmatically as follows
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(0.f, 0.f, 50.f, 50.f);
[button1 setTitle:#"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:#selector(goToFirstTrailer:)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
imageView.userInteractionEnabled = YES; // <--- this has to be set to YES
[imageView addSubview:button1];
[self.window makeKeyAndVisible];
return YES;
}
It is quick and dirty and yes, I am misusing the application delegate as the view controller. I know it is bad.
Here is the action method
- (void) goToFirstTrailer:(id)sender {
imageView.backgroundColor = [UIColor redColor];
}
Setting the userInteractionEnabled property on the parent imageView makes the difference. With it set to NO which is the default, no events are routed to the button.
If myImageView is a UIImageView object, adding a subview to it (like UIButton) only displays the image that object is represented by. If you add that UIButton as a subview of a UIView, it should work perfectly. Try adding an auxiliary UIView to your UIImageView and then add your button to a subview of the new auxiliary UIView, that should solve your problem.
This works fine for me. Note that I changed the event to UIControlEventTouchUpInside to allow the button press down state to be visible first.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20,400,88,35); //The position and size of the button (x,y,width,height)
[btn setTitle:#"Quit" forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(showAbout)
forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
[self.view addSubview:btn];
I have made a UIView which contains a table and some buttons. This view is called by a UIViewController which is called by a tab-bar controller. There is no IB involved.
When the view comes up it only has the outlines of the objects. e.g. the following button comes out without its label:
brect = CGRectMake(80, 330, 60, 27);
centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:brect];
[button2 setCenter:centern];
[button2 setTitle:#"Delete" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(deldate:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button2];
As soon as I make the first touch everything appears as it should.
What am I doing wrong?
[self.view addSubview:button2];
self is an instance of UIViewController.
self contain a view. you should add you UIButton to view, not viewController.