Changing the UIView frame does not update its frame in its parent subviews array. Hence, the position does not change

I am new to iOS app development. I have a view with 2 UIViews and 1 ScrollView. One of the UIViews is a footer that I want to animate in such a way that it hides / shows / exits the screen based on some state. However, when I change the footer frame (show / hide footer) nothing happens.

So, I did NSLog of the footer and self.view.subviews [2] and found that the frame of the footer changed successfully, but the same change was not reflected in self.view.

If I do [self.view addSubview: footer] every time I call hideFooter / showFooter solves my problem, but it doesn't work well with my scroll view (which I can't fix, so it's not an option).

Am I missing something? Is there a way to change the UIView frame so the changes are reflected in the UI as well? (I've read most of the answers to changing the UIView framework. Nothing seems to work without adding the UIView again in self.view).

Note that I have declared the footer globally in the class only 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, the animation works fine, but my scrollView gets screwed up (new sub-items added are no longer perceived).

Also I am having a similar problem with play, pause and next button. These buttons will be enabled or disabled from time to time. When I change my .enable property, that change is not reflected in self.view again.

Please help me understand the reason for this unusual behavior.

+3


source to share





All Articles