簡體   English   中英

自定義UIViews的奇怪行為

[英]Custom UIViews strange behaviour

我在我的應用程序的ViewController中實現了一個Custom View ,我用不同的參數調用了兩次,但是這里有一個奇怪的行為。

正如您在圖片中看到的,兩個views都是一組UILabel和2個UIImageView ,兩個Custom View都是相同的,實際上是創建這兩個視圖的相同代碼。

在此輸入圖像描述

我用顏色知道每個Label並查看background 灰色和橙色是2 backgrounds 第一個視圖就可以了,但第二個屬性在真實視圖之外(灰色視圖)。 任何人都知道像我這樣實例化視圖是否有任何問題? 有沒有更好的方法?

我已使用此代碼在ViewController實例化此views

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

謝謝

嘗試這個

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

當你在超級視圖中定位視圖時,看起來你可能正在混淆framebounds frame是在superview的坐標中表示的邊界矩形, bounds是在視圖自己的坐標中表示的相同的矩形。 如果上海華正好位於它的父的起源,為您的橙色觀點可能是,那么就沒有區別。 另一方面,灰色視圖的原點取決於橙色視圖的高度。 當您使用該frame來定位sherpaViewsherpaView被移位相同的數量。

如果想要視圖的坐標如果想要視圖frame 的坐標,請使用bounds

我在跟蹤您的代碼時遇到了一些麻煩,但我認為如果您更改此代碼:

CGRect sherpaFrame = self.sherpaViewDescubridor.frame;

對此:

CGRect sherpaFrame = self.sherpaViewDescubridor.bounds;

你可能會得到你想要的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM