I want to add two buttons to a subview that appears over the parent view when the view will appear. The buttons are initialized correctly and appear but they don´t react when pressed. Does anybody know why? Here is my code:
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor blackColor];
//Animation View
imageView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 499, 320, 0)];
imageView.image = [UIImage imageNamed:#"trans.png"];
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeBottom];
[self.view addSubview:imageView];
[UIView animateWithDuration:1.0f
delay:0.5f
options:UIViewAnimationOptionCurveEaseOut
animations:^(void) {
imageView.frame = CGRectMake(0, 92, 320, 320);
}
completion:NULL];
[self.view bringSubviewToFront:imageView];
// Animation View Buttons
//1 Amazon
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *button=[UIImage imageNamed:#"button.png"];
[button1 setImage:button forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 290, 80, 30);
button1.backgroundColor=[UIColor whiteColor];
button1.showsTouchWhenHighlighted=YES;
[button1 addTarget:self
action:#selector(openAmazon:)
forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button1];
//2 iTunes
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:button forState:UIControlStateNormal];
button2.frame = CGRectMake(82, 290, 80, 30);
button2.backgroundColor=[UIColor whiteColor];
button2.showsTouchWhenHighlighted=YES;
[button2 addTarget:self
action:#selector(openiTunes:)
forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button2];
}
you need to use the option UIViewAnimationOptionAllowUserInteraction
...
options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
...
Make sure you set the userInteractionEnabled property on the imageView:
[imageView setUserInteractionEnabled:YES];
[self.view bringSubviewToFront:imageView];
you are bringing your image view to the front after increasing its frame, this is blocking the buttons actions
Related
I'm currently trying to add a button to my app. If the user push on it, it should display an alert.
I want to add the button to a section header of a tableview. Here is my code in tableView:viewForHeaderInSection:
UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
And in the same class, I have the method :
-(void)showMyAlert{
NSLog(#"HERE SHOW ALERT");
...
}
The button is correctly displayed but the showMyAlert method is never called when I push on it.
Does anyone have an idea of what is wrong with my code ?
Thanks if you could help me :)
Change your UIImageView as UIView and return it as the header view.
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.
Modified below lines of code, Try like this:-
[button addTarget:self action:#selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)showMyAlert:(id)sender{
NSLog(#"HERE SHOW ALERT");
...
}
Try this, in viewDidLoad,
-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}
AppDelegate.m Navbar Customization:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor colorWithRed:(56/255.0) green:(61/255.0) blue:(63/255.0) alpha:1],
UITextAttributeTextShadowColor: [UIColor grayColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"ProximaNova-Bold" size:15.0f]
}];
LViewController.m Navbar Custom Left/Right Buttons:
// Left Button
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:#"menu"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(openLeftMenu) forControlEvents:UIControlEventTouchUpInside];
[leftButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
// Right Button
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:[UIImage imageNamed:#"location"] forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(openRightMenu) forControlEvents:UIControlEventTouchUpInside];
[rightButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
Result (Title not Centered):
I tried a lot of tips, but anyone worked:
Working with custom fonts on iOS is fraught with peril when it comes to vertical orientation of the text. Your best bet here is to embed the title in a UILabel, then place that UILabel in a UIView, positioned appropriately. Something like this:
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Test";
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f,
0.0f,
320.0f,
44.0f)];
titleLabel.frame = …; // Compute the frame you need based on the chosen font, set
// appropriately.
[container addSubview:titleLabel];
self.navigationItem.titleView = container;
-(IBAction)actionButtonPressed:(id)sender {
//Write the code to show the buttons etc like play
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionAllowAnimatedContent
animations:^{
[self.actionBtn0 setFrame:CGRectMake(25, 25, 100, 100)];
[self.actionBtn1 setFrame:CGRectMake(40, 40, 70, 70)];
[self.actionBtn2 setFrame:CGRectMake(0, 0, 150, 150)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
playBtn.alpha = 1.0;
}
completion:NULL];
}];
}
I have a button that when the app loads it is large and in the centre of the screen. However, when it is pressed i want it to shrink down in size and move to the top right of the screen. There are actually 3 buttons but they all share the same IBAction shown above.
The problem I am having is that depending on when the button is pressed the images of the buttons morph into random weird shapes that i did not specify. The images should be perfectly circular but they tend to be redrawn elliptically. Any idea how to stop this?
The original dimensions of the buttons before the button is pressed are below.
actionBtn0 = [[UIButton alloc] initWithFrame:CGRectMake(60, 185, 200, 200)];
[actionBtn0 setImage:[UIImage imageNamed:#"bigLineRing.png"]
forState:UIControlStateNormal];
[actionBtn0 addTarget:self action:#selector(actionButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn0];
[self.view bringSubviewToFront:actionBtn0];
actionBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(90, 215, 140, 140)];
[actionBtn1 setImage:[UIImage imageNamed:#"smallLineRing.png"]
forState:UIControlStateNormal];
[actionBtn1 addTarget:self action:#selector(actionButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn1];
[self.view bringSubviewToFront:actionBtn1];
actionBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(10, 135, 300, 300)];
[actionBtn2 setImage:[UIImage imageNamed:#"theEye.png"] forState:UIControlStateNormal];
[actionBtn2 addTarget:self action:#selector(actionButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn2];
[self.view bringSubviewToFront:actionBtn2];
Instead of using this creation of UIButton;
actionBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(90, 215, 140, 140)];
[actionBtn1 setImage:[UIImage imageNamed:#"smallLineRing.png"] forState:UIControlStateNormal];
try to use this:
actionBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
actionBtn1.frame = CGRectMake(90, 215, 140, 140);
[actionBtn1 setImage:[UIImage imageNamed:#"smallLineRing.png"] forState:UIControlStateNormal];
I create UIView and then I add UIButton as subview with action, but this action is never called.
Does anybody know why?
Here is my code:
- (void)viewDidLoad{
UIView *roundResultView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 150)];
UIImageView *_popUpBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NotificationBg.png"]];
_popUpBg.frame = CGRectMake(0, 0, roundViewWidth, roundViewHeight);
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 100, 30)];
[closeButton addTarget:self action:#selector(closeAndBack) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Back") forState:UIControlStateNormal];
[roundResultView addSubview:_popUpBg];
[roundResultView addSubview:closeButton];
[self.view addSubview:roundResultView];
}
-(void)closeAndBack{
NSLog(#"closeAndBack"); //never called
}
I have checked this:
NSLog(#"%#", [closeButton allControlEvents]);
NSLog(#"%#", [closeButton allTargets]);
and it prints:
... Sedmica[2192:14f03] 64
... Sedmica[2192:14f03] {(
<ViewController: 0x8631e10>
)}
Update your code with this and kindly let me know :
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[closeButton addTarget:self
action:#selector(closeAndBack:)
forControlEvents:UIControlEventTouchDown];
[closeButton setTitle:#"Back"
forState:UIControlStateNormal];
closeButton.frame = CGRectMake(30.0, 30.0, 100.0, 30.0);
[self.view addSubview:closeButton];
- (IBAction)closeAndBack:(UIButton *)button{
NSLog(#"Button clicked.");
}
This is my code.
- (void)viewWillAppear:(BOOL)animated
{
customView = [[UIView alloc] init];
UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(410, -20, 30, 40)];
[btnAdd setBackgroundColor:[UIColor blackColor]];
[btnAdd setBackgroundImage:[UIImage imageNamed:#"oie_815362uTrAOBMX.png"] forState:UIControlStateNormal];
[btnAdd addTarget:nil action:#selector(addCustomer:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnAdd];
UIButton *btnJump = [[UIButton alloc] initWithFrame:CGRectMake(450, -20, 30, 40)];
[btnJump setBackgroundColor:[UIColor blueColor]];
[btnJump setBackgroundImage:[UIImage imageNamed:#"oie_8153842yVgkR6XD.jpg"] forState:UIControlStateNormal];
[btnJump addTarget:nil action:#selector(showMenue:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnJump];
UIButton *btnBarItem = [[UIButton alloc] initWithFrame:CGRectMake(300, -20, 100, 40)];
[btnBarItem setBackgroundColor:[UIColor greenColor]];
[btnBarItem setBackgroundImage:[UIImage imageNamed:#"oie_99342oliBc5Sy.jpg"] forState:UIControlStateNormal];
[btnBarItem addTarget:nil action:#selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnBarItem];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, -10, 30, 30)];
[imageView setImage:[UIImage imageNamed:#"oie_972931iW4SHiZU.jpg"]];
[customView addSubview:imageView];
UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -20, 80, 40)];
[mainLabel setText:#"Kunden"];
[mainLabel setBackgroundColor:[UIColor lightGrayColor]];
[mainLabel setTextColor:[UIColor blackColor]];
[mainLabel setFont:[UIFont fontWithName:#"Arial" size:18]];
[customView addSubview:mainLabel];
[self.navigationItem setTitleView:customView];
}
- (IBAction)showMenue:(id)sender
{
NewPopUpmenu *options = [[NewPopUpmenu alloc] initWithNibName:#"PopupMenu" bundle:nil];
[options.view setFrame:CGRectMake(720, 0, 300, 200)];
[self.view addSubview:options.view];
}
Note: Problem is that when i press on my "jump" button it doesn't access it's selector Method (showMenue:).
You are specifying a target of nil for each button's target-action pair. If your action method is implemented in the same class, your target should be self:
[btnAdd addTarget:self
action:#selector(addCustomer:)
forControlEvents:UIControlEventTouchUpInside];
// ...
// Same for other buttons...
// ...
Technically, if you specify nil for the target parameter, the responder chain is searched for an object that responds to the action. The problem you are having could possibly be down to the fact that your showeMenu: method isn't the signature that your action is expecting. The action is looking for a method signature that looks like this:
- (void)showeMenu:(id)sender;
Yet you have specified the IBAction return type in the implementation.