I am fairly new to IOS App Development. I have a view with 2 UIView and 1 ScrollView. One of the UIView is a footer which I want to be animated in such a way that it will hide/show to/from out of the screen based on some condition. However when I change the frame of the footer(show/hide footer), nothing happens.
So I did a NSLog of the footer and the self.view.subviews[2] and found that the frame of footer has changed successfully but the same change has not been reflected in the self.view.
If I do [self.view addSubview:footer] every time I call hideFooter/showFooter solves my problem but it messes up bad with my scroll view (which I cannot rectify, so this is not an option).
Am I missing something here? Is there a way to modify frame of a UIView such that the changes reflect in the UI too? (I have read most of the answers on modifying frame of a UIView. None seems to work without adding the UIView again to self.view).
Note that I have declared footer globally in the class just inside @implementation and have initialized it only once inside a method called from viewDidLoad.
@implementation PlayAudioViewController
UIButton *play;
UIButton *next;
UIButton *previous;
UIView* header;
UIView* footer;
- (void)viewDidLoad
{
[self addFooter];
}
-(void)addFooter{
footer = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height * 1, self.view.frame.size.width, self.view.frame.size.height * 0.1)];
UIImageView *headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.1)];
headerImage.image = [UIImage imageNamed:@"Wooden-Background.jpg"];
[footer addSubview:headerImage];
footer.backgroundColor = [UIColor redColor];
queue = [[NSMutableArray alloc] init];
play = [[UIButton alloc]initWithFrame:CGRectMake(footer.frame.size.width * 0.475, footer.frame.size.height * 0.10, footer.frame.size.height * 0.80, footer.frame.size.height * 0.80)];
[play setBackgroundImage:[UIImage imageNamed:@"play_1.png"] forState:UIControlStateNormal];
[play setBackgroundImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateSelected];
[play addTarget:self action:@selector(universalplay:) forControlEvents: UIControlEventTouchUpInside];
play.enabled = YES;
[footer addSubview:play];
next = [[UIButton alloc]initWithFrame:CGRectMake(footer.frame.size.width * 0.675, footer.frame.size.height * 0.20, footer.frame.size.height * 0.60, footer.frame.size.height * 0.60)];
[next setBackgroundImage:[UIImage imageNamed:@"next.png"] forState:UIControlStateNormal];
[next addTarget:self action:@selector(nextplay:) forControlEvents: UIControlEventTouchUpInside];
next.enabled=NO;
[footer addSubview:next];
previous = [[UIButton alloc]initWithFrame:CGRectMake(footer.frame.size.width * 0.29, footer.frame.size.height * 0.20, footer.frame.size.height * 0.60, footer.frame.size.height * 0.60)];
[previous setBackgroundImage:[UIImage imageNamed:@"previous.png"] forState:UIControlStateNormal];
[previous addTarget:self action:@selector(previousplay:) forControlEvents: UIControlEventTouchUpInside];
previous.enabled=NO;
[footer addSubview:previous];
NSLog(@"%@", footer.subviews[1]);
[self.view addSubview:footer];
[self hideFooter];
}
-(void)showFooter{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
footer.frame = CGRectMake(0, self.view.frame.size.height * 0.9, self.view.frame.size.width, self.view.frame.size.height * 0.1);
//[self.view addSubview:footer];
[UIView commitAnimations];
NSLog(@"%@", self.view.subviews[0]);
NSLog(@"%@", self.view.subviews[1]);
NSLog(@"%@", ((UIButton *)((UIView *)self.view.subviews[2]).subviews[1]));
}
-(void)hideFooter{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
footer.frame = CGRectMake(0, self.view.frame.size.height * 1, self.view.frame.size.width, self.view.frame.size.height * 0.1);
//[self.view addSubview:footer];
[UIView commitAnimations];
}
As stated earlier, if I uncomment the commented statement in addFooter and showFooter, animation works perfectly but my scrollView gets screwed(newly added subviews stop getting reflected).
Also I am facing similar problem with play, pause and next button. These buttons will be enabled or disabled time to time. When I am changing their .enable property, the change is again not reflected in the self.view.
Please help me understand the reason behind this unusual behavior.