我在我的应用的Custom View
内实现了一个ViewController
,我用不同的参数调用了两次,但这里有一个奇怪的行为。
正如您在图片中看到的,views
是一组UILabel
和2 UIImageView
s,两个Custom View
都是等于的,实际上是相同的代码创建这两个视图。
我使用颜色知道每个Label
的位置并查看background
。灰色和橙色是2 backgrounds
。第一个视图就可以了,但第二个属性在真实视图之外(灰色视图)。任何人都知道像我这样实例化视图是否有任何问题?还有更好的办法吗?
我已使用此代码在views
中实例化此ViewController
:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.sherpaViewDescubridor];
[self.view addSubview:self.sherpaViewConquistador];
}
#pragma mark - Custom Getters
-(WPSherpaView *)sherpaViewDescubridor
{
if(!sherpaViewDescubridor){
CGRect sherpaFrame = CGRectMake(10, 0, 320, 125);
sherpaViewDescubridor = [[WPSherpaView alloc] initWithFrame:sherpaFrame];
return sherpaViewDescubridor;
}
return sherpaViewDescubridor;
}
-(WPSherpaView *)sherpaViewConquistador
{
if(!sherpaViewConquistador){
CGRect sherpaFrame = self.sherpaViewDescubridor.frame;
sherpaFrame.origin.y = CGRectGetMaxY(sherpaFrame) + 5;
sherpaViewConquistador = [[WPSherpaView alloc] initWithFrame:sherpaFrame];
sherpaViewConquistador.backgroundColor = [UIColor grayColor];
return sherpaViewConquistador;
}
return sherpaViewConquistador;
}
这是自定义视图代码:
@implementation WPSherpaView
@synthesize aliasLbl, nameLbl ,pointsLbl,positionLbl,profileImg,flagImg, viewNameLbl, warCryLbl;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect firstLabelFrame = {
.origin.x = kMarginLeft,
.origin.y = self.frame.origin.y + 10.0 ,
.size.width = self.frame.size.width - (2*kMarginLeft),
.size.height = kNameViewHeight,
};
viewNameLbl = [[WPCustomLabel alloc] initWithFrame:firstLabelFrame];
[viewNameLbl setFont:[UIFont systemFontOfSize:KMediumFontSize]];
[viewNameLbl setBackgroundColor:[UIColor redColor]];
viewNameLbl.textAlignment = NSTextAlignmentLeft;
[self addSubview:viewNameLbl];
CGRect centreViewFrame = firstLabelFrame;
centreViewFrame.origin.y = CGRectGetMaxY(firstLabelFrame) + 10.0;
UIView *centerView = [[UIView alloc] initWithFrame:centreViewFrame];
centerView.backgroundColor = [UIColor greenColor];
[self addSubview:centerView];
// LEFT IMAGES
CGRect imageProfileFrame = {
.origin.x = 0,
.origin.y = 0 ,
.size.width = kImageSize,
.size.height = kImageSize,
};
profileImg = [[UIImageView alloc] initWithFrame:imageProfileFrame];
profileImg.backgroundColor = [UIColor blueColor];
[centerView addSubview:profileImg];
imageProfileFrame.origin.x = CGRectGetMaxX(imageProfileFrame)+ kMarginLeft;
flagImg = [[UIImageView alloc] initWithFrame:imageProfileFrame];
flagImg.backgroundColor = [UIColor orangeColor];
[centerView addSubview:flagImg];
// RIGHT LABELS
CGRect labelFrame = imageProfileFrame;
labelFrame.origin.x = CGRectGetMaxX(imageProfileFrame) + (2 * kMarginLeft);
labelFrame.size = kSizelabel;
aliasLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[aliasLbl setFont:[UIFont systemFontOfSize:KMediumFontSize]];
[aliasLbl setBackgroundColor:[UIColor blackColor]];
aliasLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:aliasLbl];
labelFrame.origin.y += kSizelabel.height;
labelFrame.size.height = 15.0;
nameLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[nameLbl setFont:[UIFont systemFontOfSize:KMinFontSize]];
[nameLbl setBackgroundColor:[UIColor grayColor]];
nameLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:nameLbl];
labelFrame.origin.y += kSizelabel.height;
positionLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[positionLbl setFont:[UIFont systemFontOfSize:13]];
[positionLbl setBackgroundColor:[UIColor orangeColor]];
positionLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:positionLbl];
labelFrame.origin.y += kSizelabel.height;
warCryLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[warCryLbl setFont:[UIFont systemFontOfSize:13]];
[warCryLbl setBackgroundColor:[UIColor blackColor]];
warCryLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:warCryLbl];
centreViewFrame.size.height = CGRectGetMaxY(labelFrame) + 5 ;
centerView.frame = centreViewFrame;
// BORDER VIEW
CGRect bordeFrame = self.frame;
bordeFrame.size.height = 0.5;
bordeFrame.origin.y = self.frame.size.height - 0.5;
bordeFrame.origin.x = kMarginLeft;
UIView *bottomBorder = [[UIView alloc] initWithFrame:bordeFrame];
bottomBorder.backgroundColor = [UIColor grayColor];
[self addSubview:bottomBorder];
}
return self;
}
谢谢
答案 0 :(得分:2)
试试这个
CGRect firstLabelFrame = {
.origin.x = kMarginLeft,
.origin.y = self.bounds.origin.y + 10.0 , // <----
.size.width = self.bounds.size.width - (2*kMarginLeft), // <-----
.size.height = kNameViewHeight,
};
答案 1 :(得分:1)
当您将视图置于超级视图中时,您可能会混淆frame
和bounds
。 frame
是在superview的坐标中表示的边界矩形,bounds
是在视图自己的坐标中表示的相同矩形。如果superview恰好位于其 superview的原点,正如您的橙色视图可能是,则没有区别。另一方面,灰色视图的原点取决于橙色视图的高度。当您使用frame
找到sherpaView
时,sherpaView
会被移位相同数量。
如果您想要视图中的坐标和bounds
,如果您想要视图的坐标,请使用frame
。
我在跟踪您的代码时遇到了一些麻烦,但我认为如果您更改此代码:
CGRect sherpaFrame = self.sherpaViewDescubridor.frame;
到此:
CGRect sherpaFrame = self.sherpaViewDescubridor.bounds;
你可能会得到你想要的结果。