Search code examples
iosuinavigationbarsubviewuistatusbar

iOS How to slide in view from bottom, accounting for the navigation bar + status bar height


I'm trying to display a custom view like an action sheet in a root view controller within a UINavigationController, but I always end up having the bottom obscured because I don't account for the UINavigationBar + status bar height.

For example the following code will have 64 points worth of bottom part of customView obscured:

CGFloat height = [UIScreen mainScreen].bounds.size.height;

[UIView animateWithDuration:0.5 animations:^{
    customView.center = CGPointMake(width / 2.0, height - customViewHeight / 2.0);
}];

I could just use setFrame instead of center but that is irrelevant because it will give me the same results more or less.

I know using constants like -64 pts is not desirable, so is there a away to convert points to the actual screen coordinates? What is the best practice?

Edit (Possibly an additional question)

I'm trying to have a view from outside the bottom of the screen slide in like an UIActionSheet, and every time I try to do this, I always run into the same problem.

I try to set the initial subview frame origin.y to the height of the screen, and have the subview animate to (screenHeight - subviewHeight), which is why I always run into my problem of having the bottom 64 CGPoints obscured.

What's the best way to do this kind of implementation? I've tried searching for sample code, but I've only seen codes using height like CGPoint height = 480.0 - subviewHeight;, which I heard is poor practice.


Solution

  • One way to do this

    @implementation UIView (XXScreenConversions)
    - (CGPoint)xx_pointFromScreenPoint:(CGPoint)point {
        CGPoint inWindow = [self.window convertPoint:point fromWindow:nil];
        return [self.window convertPoint:inWindow toView:self];
    }
    
    - (CGPoint)xx_screenPointFromPoint:(CGPoint)point {
        CGPoint inWindow = [self.window convertPoint:point fromView:self];
        return [self.window convertPoint:inWindow toWindow:nil];
    }
    @end