简体   繁体   中英

Find real frame of UIVIew

I have a custom UIView I create in my class. I then add it to my screen and I want to know it's CGRect . But from what it seems, I can't seem to get the size of this particular UIView . Here's my code:

CGRect labelFrame;
        if (!tickerImage) {
            labelFrame = CGRectMake(upperTextField.frame.origin.x + 8, upperTextField.frame.origin.y, 0, 0);
        } else {
            labelFrame = CGRectMake(upperTextField.frame.origin.x + 16, upperTextField.frame.origin.y, 0, 0);
        }

        SGNewLabel = [[SGAdressLabel alloc] initWithFrame:labelFrame];
        [labelsArray addObject:SGNewLabel];
        [self.view addSubview:SGNewLabel];

        [SGNewLabel addNewLabelWithText:labelText andTickerImage:tickerImage];

        UIView *lastLabel = [labelsArray lastObject];
        CGRect newTextFieldFrame = CGRectMake(lastLabel.frame.origin.x + lastLabel.frame.size.width, labelFrame.origin.y, 320, 30);
        upperTextField.frame = newTextFieldFrame;

As you see, I set the upperTextField frame based on the frame of my custom SGNewLabel which appears to be wrong, even if put into NSArray . I set 0, 0 at the end of the labelFrame declaration because I don't know the size of the future object and here is the source of my problem.

How can I correct it and get the right size of my UIView?

EDIT : NSLog on my view gives me this:

NSLog(@"The rect of object is: %@", NSStringFromCGRect(self.frame));
2013-02-11 17:16:02.333 MyApp[2383:c07] The rect of object is: {{24, 55}, {0, 0}}

The UIView itself is drawn normally and I can see it full-sized.

I should have used my label's frame set in its own class and then my code would look like this:

SGNewLabel = [[SGAdressLabel alloc] init];
CGRect labelFrame;
        if (!tickerImage) {
            labelFrame = CGRectMake(upperTextField.frame.origin.x + 8, upperTextField.frame.origin.y, SGNewLabel.frame.size.width, SGNewLabel.frame.size.height);
        } else {
            labelFrame = CGRectMake(upperTextField.frame.origin.x + 16, upperTextField.frame.origin.y, SGNewLabel.frame.size.width, SGNewLabel.frame.size.height);
        }
SGNewLabel.frame = labelFrame;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM