UIButton changes shape when pressed - objective-c

-(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];

Related

customizing the titleView of navigation bar in iOS7?

Hello all I want to customize titleView of navigation bar by adding three buttons on navigation bar in iOS7 below is my code but nothing happens
-(void)addCutomButtonsToNavigationController:(UINavigationController *)navigationController
{
UIView *customNavigationTitleView=[[UIView alloc] initWithFrame:CGRectMake(20, 0, 250, 44)];
[customNavigationTitleView setBackgroundColor:[UIColor blackColor]];
UIButton *friendRequestButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *messagesButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *notificationsButton=[UIButton buttonWithType:UIButtonTypeCustom];
[friendRequestButton setImage:[UIImage imageNamed:#"ic_clients.png"] forState:UIControlStateNormal];
[messagesButton setImage:[UIImage imageNamed:#"ic_location.png"] forState:UIControlStateNormal];
[notificationsButton setImage:[UIImage imageNamed:#"ic_message.png"] forState:UIControlStateNormal];
[friendRequestButton setFrame:CGRectMake(0, 10, 40, 40)];
[customNavigationTitleView addSubview:friendRequestButton];
[customNavigationTitleView addSubview:messagesButton];
[customNavigationTitleView addSubview:notificationsButton];
navigationController.navigationItem.titleView =customNavigationTitleView;
}
Try this
-(void)addCutomButtonsToNavigationController:(UINavigationController *)navigationController
{
UIView *customNavigationTitleView=[[UIView alloc] initWithFrame:CGRectMake(40, 0, 160, 44)];
UIButton *friendRequestButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *messagesButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *notificationsButton=[UIButton buttonWithType:UIButtonTypeCustom];
[friendRequestButton setImage:[UIImage imageNamed:#"ic_clients.png"] forState:UIControlStateNormal];
[messagesButton setImage:[UIImage imageNamed:#"ic_location.png"] forState:UIControlStateNormal];
[notificationsButton setImage:[UIImage imageNamed:#"ic_messsage.png"] forState:UIControlStateNormal];
[friendRequestButton setFrame:CGRectMake(10, 0, 40, 40)];
[messagesButton setFrame:CGRectMake(60, 0, 40, 40)];
[notificationsButton setFrame:CGRectMake(110, 0, 40, 40)];
[customNavigationTitleView addSubview:friendRequestButton];
[customNavigationTitleView addSubview:messagesButton];
[customNavigationTitleView addSubview:notificationsButton];
self.navigationItem.titleView =customNavigationTitleView;
}

Stop UIButton turning blue when pressed

In my iPad app I have a UIButton that when pressed initiates some data transfer which takes time. During this time the button turns blue as shown:
Is there anyway of changing this colour?
Change the tint color, it will be used when button is highlighted.
[myButton setTintColor:[UIColor blackColor]];
Try This
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 100, 50)];
[button setTitle:#"hi" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:button];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
button.backgroundColor = [UIColor whiteColor];
[UIView commitAnimations];

iPhone UIButton action is not working

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.");
}

Buttons that aren't a subview of UIView don't work

If I change it to be a subview of timerView it works fine but nothing else works.
timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 33)];
[self.view addSubview:timerView];
UIImageView *changerBackground = [[UIImageView alloc] init];
[changerBackground setImage:[UIImage imageNamed:#"speedbackground.png"]];
changerBackground.frame = CGRectMake(-12 , -16, 105, 33);
[timerView addSubview:changerBackground];
UIButton *normal = [UIButton buttonWithType:UIButtonTypeCustom];
normal.frame = CGRectMake(40, 8, 22, 22);
[normal setBackgroundImage:[UIImage imageNamed:#"normalwhite.png"] forState:UIControlStateNormal];
[normal setBackgroundImage:[UIImage imageNamed:#"normalred.png"] forState:UIControlStateHighlighted];
[normal addTarget:self action:#selector(quickTest:) forControlEvents:UIControlEventTouchUpInside];
[changerBackground addSubview:normal];
[changerBackground setUserInteractionEnabled:YES];
will solve your issue. As the UIImageView disables the user interaction by default.

UIButton not working on animated subView

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