Gabriel Ortega
Gabriel Ortega

Reputation: 481

Custom UIView Placement within Superview

I have a custom UIView subclass that needs to be at the bottom of the superview. I set the view's origin using:

CGRect subviewFrame = subview.frame;

CGPoint newOrigin = CGPointMake(0, superview.bounds.size.height - subviewFrame.size.height);

subviewFrame.origin = newOrigin;

[subview setFrame:subviewFrame];

However, this places the subview (origin.y) directly outside of the superview's view frame.

Subview outside of frame

If I use:

CGPoint newOrigin = CGPointMake(0, superview.bounds.size.height - subviewFrame.size.height * 2.0f);

I get the results the I want, which is the subview sitting on the bottom of the window.

Subview inside of frame

I don't see why I have to multiply the subview's height by 2.

If someone could tell me what I'm missing it would be greatly appreciated. Thanks!

Upvotes: 1

Views: 371

Answers (2)

hrchen
hrchen

Reputation: 1223

Useful code to draw bounds of UIView. KADebugShowViewBounds(superview, [UIColor redColor]) and KADebugShowViewBounds(subview, [UIColor redColor]). You will see the bounds of superview and subview in red color. I think the problem should be your layout.

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

#ifndef KAViewDebugHelper_h
#define KAViewDebugHelper_h

#ifdef DEBUG
#define KADebugShowViewBounds(aView, aColor) \
do \
{ \
    UIColor *color = [(id)aColor isKindOfClass:[UIColor class]] ? aColor : [UIColor redColor]; \
    aView.layer.borderColor = color.CGColor; \
    aView.layer.borderWidth = 1.0f; \
} \
while(0)
#else
#define KADebugShowViewBounds(aView)
#endif
#endif

Upvotes: 1

Gabriel Ortega
Gabriel Ortega

Reputation: 481

I was using [UIScreen mainScreen].bounds to get the initial frame, which does not compensate for the status and navigation bars. UIScreen applicationFrame: returns the frame minus the status bar height. I use this method and take into account the nav bar height (44.0) to get the desired results.

Upvotes: 1

Related Questions