Ios 风俗习惯对奇怪行为的看法

Ios 风俗习惯对奇怪行为的看法,ios,iphone,objective-c,uiview,Ios,Iphone,Objective C,Uiview,我在我的应用程序的视图控制器中实现了一个自定义视图,我用不同的参数调用了两次,但其中有一个奇怪的行为 如图所示,两个视图都是一组UILabels和2个UIImageViews,两个自定义视图s都是相等的,实际上创建这两个视图的代码是相同的 我用颜色知道每个标签的位置,并查看背景。灰色和橙色是2背景。第一个视图在其位置上是正常的,但第二个属性在真实视图(灰色视图)之外。有人知道像我这样实例化视图是否有问题吗?还有更好的办法吗 我已使用此代码在ViewController中实例化此视图: - (v

我在我的应用程序的
视图控制器
中实现了一个
自定义视图
,我用不同的参数调用了两次,但其中有一个奇怪的行为

如图所示,两个
视图
都是一组
UILabel
s和2个
UIImageView
s,两个
自定义视图
s都是相等的,实际上创建这两个视图的代码是相同的

我用颜色知道每个
标签的位置
,并查看
背景
。灰色和橙色是2
背景。第一个视图在其位置上是正常的,但第二个属性在真实视图(灰色视图)之外。有人知道像我这样实例化视图是否有问题吗?还有更好的办法吗

我已使用此代码在
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;
}

谢谢

当您在superview中定位视图时,您可能混淆了
边界
frame
是以superview坐标表示的边界矩形,
bounds
是以视图自身坐标表示的相同矩形。如果superview恰好位于其superview的原点,就像您的橙色视图一样,则没有区别。另一方面,灰色视图的原点被橙色视图的高度所取代。当您使用该
来定位您的
sherpaView
时,
sherpaView
会被等量的位移

如果需要视图内部的坐标,请使用
bounds
;如果需要视图的坐标,请使用
frame

我在遵循您的代码时遇到了一些问题,但我认为如果您更改此代码:

CGRect sherpaFrame = self.sherpaViewDescubridor.frame;
为此:

CGRect sherpaFrame = self.sherpaViewDescubridor.bounds;
你可能会得到你想要的结果。

试试这个

CGRect firstLabelFrame = {
    .origin.x = kMarginLeft,
    .origin.y =  self.bounds.origin.y + 10.0 , // <----
    .size.width = self.bounds.size.width - (2*kMarginLeft), // <-----
    .size.height = kNameViewHeight,
};
CGRect firstLabelFrame={
.origin.x=kMarginLeft,

.origin.y=self.bounds.origin.y+10.0,//我认为它正在创建两个SherpaviWeconquisitor。一个背景颜色为灰色,没有元素。另一个元素没有颜色。即使这样做
它仍然会这样做。这是完整的代码吗?因为问题与小的(彩色的)相关视图,而不是大视图。您发布的代码只创建了大视图,并且它们的位置正确。不,我也有大视图。但是如果第一个视图是正确的,为什么第二个视图不正确?它是同一个代码,您想看它吗?是的,请发布它,因为没有人能在当前代码中发现任何问题。很可能是您混淆了
框架
边界
在代码中的某个地方有一些小的,正如一些人提到的。谢谢。它已经起作用了。我不太清楚边界和框架之间的区别。Caleb已经很好地解释了。但是我不太明白。请确保您阅读文档,因为边界/框架的差异非常重要。Basically
frame
是视图相对于其父视图的位置;
bounds
始终具有原点(0,0),除非视图可滚动,并且与其父视图无关。