[英]Custom UIView Placement within Superview
我有一个自定义UIView子类,该子类需要位于超级视图的底部。 我使用以下方法设置视图的原点:
CGRect subviewFrame = subview.frame;
CGPoint newOrigin = CGPointMake(0, superview.bounds.size.height - subviewFrame.size.height);
subviewFrame.origin = newOrigin;
[subview setFrame:subviewFrame];
但是,这会将子视图(origin.y)直接放置在超级视图的视图框架之外。
如果我使用:
CGPoint newOrigin = CGPointMake(0, superview.bounds.size.height - subviewFrame.size.height * 2.0f);
我得到了想要的结果,即位于窗口底部的子视图。
我不明白为什么我必须将子视图的高度乘以2。
如果有人能告诉我我所缺少的内容,将不胜感激。 谢谢!
我正在使用[UIScreen mainScreen] .bounds获取初始帧,该帧不能补偿状态栏和导航栏。 UIScreen applicationFrame:返回减去状态栏高度的框架。 我使用这种方法,并考虑到导航栏的高度(44.0),以获得所需的结果。
有用的代码来绘制UIView的边界。 KADebugShowViewBounds(superview,[UIColor redColor])和KADebugShowViewBounds(subview,[UIColor redColor])。 您将看到红色的超级视图和子视图的边界。 我认为问题应该出在您的布局上。
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.