ios - 自定义 UIViews 奇怪的行为

标签 ios iphone objective-c uiview

我正在实现一个 Custom ViewViewController 内我用不同的参数调用了两次我的应用程序,但这有一个奇怪的行为。

正如您在图片中看到的 views是一组UILabel s 和 2 UIImageView s,两个 Custom View s 是相等的,实际上创建这两个 View 的代码相同。

enter image description here

我用颜色知道每个 Label 在哪里并查看 background .灰色和橙色是 2 backgrounds .第一个 View 可以代替它,但第二个属性在真实 View 之外(灰色的)。任何人都知道像我一样实例化 View 是否有任何问题?有没有更好的办法?

我已使用此代码实例化此 viewsViewController :

- (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;
}

这是自定义 View 代码:
@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;
}

谢谢

最佳答案

试试这个

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

关于ios - 自定义 UIViews 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199235/

相关文章:

ios - 用于更改 iOS 中 slider 条粗细的 Xamarin Form slider 自定义

iphone - 如何在移动 Safari 中计算可视区域的高度(即窗口高度减去地址和书签栏)?

javascript - 如何在自动放大表单后触发 iPhone 缩小?

ios - 如何在 UIDocumentInteractionController 发送的电子邮件中设置收件人和主题字段?

iphone - '取带&' what'的地址是什么意思?

ios - 在 Objective-C block 中遇到 NSString 返回类型的问题

ios - 从内存中释放图像, swift

iphone - UITableViewCell 中的 UIImageView 大小调整为行高

ios - FBAppinvite 中的深层链接错误?

ios - 剪裁当前上下文,使其被路径掩盖