Isuru
Isuru

Reputation: 31283

Setting the frame to a UIView not working

I need a UIView to be hidden from the screen initially and then slides up. Kind of like a UIActionSheetView animation. In IB, I have set a UIView stick to the bottom border of the superview. In code, I have changed its frame to be hidden away from the screen when the app starts. And when it starts, the UIView slides up. Here's is my code. This optionsSheetView is the 'UIView' I'm referring to.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Hide the UIView from the screen initially
    self.optionsSheetView.frame = CGRectMake(0, self.optionsSheetView.frame.origin.y + self.optionsSheetView.frame.size.height, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);

    // Sliding up animation
    [UIView animateWithDuration:0.9 delay:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.optionsSheetView.frame = CGRectMake(0, 374, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
    } completion:^(BOOL finished) {

    }];
}

Now here's my problem. This works just fine. But I don't like keeping hard coded values in my code. Like 374 for the optionsSheetView's y coordinate. If I put self.optionsSheetView.frame.origin.y there, it won't work. It's basically the same thing. When I replace it with its actual value which is 374, it works fine.

Why is this happening? Is there any way I can go about this without using magic numbers?

Thank you.

Upvotes: 1

Views: 491

Answers (1)

Avt
Avt

Reputation: 17043

Try following:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGFloat oldY = self.optionsSheetView.frame.origin.y;
    // Hide the UIView from the screen initially
    self.optionsSheetView.frame = CGRectMake(0, self.optionsSheetView.frame.origin.y + self.optionsSheetView.frame.size.height, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);

    // Sliding up animation
    [UIView animateWithDuration:0.9 delay:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.optionsSheetView.frame = CGRectMake(0, oldY, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
    } completion:^(BOOL finished) {

    }];
}

Upvotes: 1

Related Questions