Search code examples
iosobjective-cuiviewtvosbounds

Bounds and Frame Size of a UIView


I need some help to set the bounds and frame of a UIView, I change it to a "full screen mode" by changing the bounds to "0.0f, 0.0f, 1920.0f, 1080.0f", that works, the problem I'm having is when I need to reverse the UIView back to the original bounds and frame size, but I'm having trouble setting this up. Can I get some help please?

CGRect rect = self.glView.frame;

            if (!CGRectEqualToRect(self.view.frame, rect))
            {
                NSLog(@"Switching to Full Screen now...");

                UIView *image = [_avplayController drawableView];

                image.frame =  CGRectMake(0.0f, 0.0f, 1920.0f, 1080.0f);

                self.glView.frame = image.frame;

            }
            else
            {
                NSLog(@"Switching to Normal Screen...");


                NSLog(@"Old Frame %@", NSStringFromCGRect(self.glView.frame));
                NSLog(@"Old Center %@", NSStringFromCGPoint(self.glView.center));

                // New Bounds and Frame size
                CGRect frame = self.glView.bounds;
                frame.size.height = 927.0f;
                frame.size.width = 619.0f;
                frame.origin.x = 931.0f;
                frame.origin.y = 65.0f;
                self.glView.bounds = frame;

                NSLog(@"New Frame %@", NSStringFromCGRect(self.glView.frame));
                NSLog(@"New Center %@", NSStringFromCGPoint(self.glView.center));

            }

Solution

  • Try with Frame instead of Bounds

    CGRect frame = self.glView.frame;
    frame.size.height = 927.0f;
    frame.size.width = 619.0f;
    frame.origin.x = 931.0f;
    frame.origin.y = 65.0f;
    self.glView.frame = frame;
    

    The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

    The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

    So, frame will help you to adjust your view within superview.

    Thanks