简体   繁体   中英

the frame.origin.y change on when I change UIVIew's bounds.size.height

(my english is not very well LOL)

i defined a UIView object as a subview, when i change the subview.bounds.size.height, the subview.frame.origin.y changed, is there something wrong in my code?

here is my code:

- (IBAction)touch
{
    static int i = 1;
    NSLog(@"%d: Bounds: %@", i, NSStringFromCGRect(self.subView.bounds));
    NSLog(@"%d: Frame: %@", i, NSStringFromCGRect(self.subView.frame));
    self.subView.bounds = CGRectMake(0.f, 0.f, self.subView.bounds.size.width, self.subView.bounds.size.height + 50);
    NSLog(@"%d: Bounds: %@", i, NSStringFromCGRect(self.subView.bounds));
    NSLog(@"%d: Frame: %@", i, NSStringFromCGRect(self.subView.frame));

    i++;
}

system output:

2011-12-23 12:11:29.316 Angry[7959:f803] 1: Bounds: {{0, 0}, {320, 100}}
2011-12-23 12:11:29.318 Angry[7959:f803] 1: Frame: {{0, 0}, {320, 100}}
2011-12-23 12:11:29.319 Angry[7959:f803] 1: Bounds: {{0, 0}, {320, 150}}
2011-12-23 12:11:29.320 Angry[7959:f803] 1: Frame: {{0, -25}, {320, 150}}

2011-12-23 12:11:30.920 Angry[7959:f803] 2: Bounds: {{0, 0}, {320, 150}}
2011-12-23 12:11:30.921 Angry[7959:f803] 2: Frame: {{0, -25}, {320, 150}}
2011-12-23 12:11:30.922 Angry[7959:f803] 2: Bounds: {{0, 0}, {320, 200}}
2011-12-23 12:11:30.923 Angry[7959:f803] 2: Frame: {{0, -50}, {320, 200}}

2011-12-23 12:11:32.080 Angry[7959:f803] 3: Bounds: {{0, 0}, {320, 200}}
2011-12-23 12:11:32.082 Angry[7959:f803] 3: Frame: {{0, -50}, {320, 200}}
2011-12-23 12:11:32.083 Angry[7959:f803] 3: Bounds: {{0, 0}, {320, 250}}
2011-12-23 12:11:32.084 Angry[7959:f803] 3: Frame: {{0, -75}, {320, 250}}

changing the bounds does indeed affect the frame:

Although you can change the frame, bounds, and center properties independent of the others, changes to one property affect the others in the following ways:

  • When you set the frame property, the size value in the bounds property changes to match the new size of the frame rectangle. The value in the center property similarly changes to match the new center point of the frame rectangle.
  • When you set the center property, the origin value in the frame changes accordingly.
  • When you set the size of the bounds property, the size value in the frame property changes to match the new size of the bounds rectangle.

I just ran into the same issue. Turns out that changing bounds on a view will resize that view relative to an anchor point that's in the center of the view. In other words, if you have a view with frame (0,0,0,0) and then change it's bounds to (0,0,100,100), the new frame will be (-50, -50, 100, 100).

The quickest workaround is remembering frame's origin before changing bounds and then setting frame's origin back to the original value afterwards:

CGPoint originBeforeResize = self.frame.origin;
self.bounds = CGRectMake(0, 0, SOME_NEW_WIDTH, SOME_NEW_HEIGHT);
self.frame = CGRectMake(originBeforeResize.x, originBeforeResize.y, 
                        self.frame.size.width, self.frame.size.height); 

I've had similar problems:

view.frame = CGRectMake(x, y, width, height); // y felt to 0

The problem was in height, I tried to set negative height. Not sure, but i've read something about tuning bad view parameters automatically, but found nothing about it on the web. So you may check your CGRect parameters.

Hope this help somebody

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