Question

I made a custom UIView with two text labels and two buttons. It is fully functional when it's created, but now I'm trying to move it when the screen rotates (i.e. from portrait to landscape).

Even though the UI elements seem to be moved correctly, the actual UIView's frame ends up doing weird things, like turn into a rectangle when it was originally a square and is being set to a square but with different x, y positions.

How I saw this is by setting my UIView's background color to black and observing that my background image was being drawn where I would expect the view to be drawn but the UIView's background becomes a different shape and is drawn in the wrong place.

Here is my code:

- (void) moveControls
{
    CGRect frame;

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation)
    {
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
            frame = CGRectMake(0, 120, 320, 320);
            break;
        case UIDeviceOrientationLandscapeLeft:
        case UIDeviceOrientationLandscapeRight:
            frame = CGRectMake(120, 0, 320, 320);
            break;
        case UIDeviceOrientationFaceDown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationUnknown:
           return;
    }

    [self setFrame:frame];

    backgroundImage.frame = CGRectMake(0,0, frame.size.width, frame.size.height);
    titleLabel.frame = CGRectMake(0,50,frame.size.width,frame.size.height/2);
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.frame = CGRectMake(0,frame.size.height/3,frame.size.width,frame.size.height/3);
    doneButton.frame = CGRectMake(frame.size.width - 154, frame.size.height - 90, 100, 34);
    tellButton.frame =CGRectMake(54, frame.size.height - 90, 100, 34);
}
Was it helpful?

Solution

Doesn't really answer all the questions but fixes the problem: ios: UIView's width and height after changing orientations is inaccurate?

view.center = view.superview.center;
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |  UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

OTHER TIPS

Use:

- (void) moveControls
{
    CGRect frame;

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (UIDeviceOrientationIsLandscape(orientation))
        frame = CGRectMake(120, 0, 320, 320);
    else
        frame = CGRectMake(0, 120, 320, 320);



    [self setFrame:frame];

    backgroundImage.frame = CGRectMake(0,0, frame.size.width, frame.size.height);
    titleLabel.frame = CGRectMake(0,50,frame.size.width,frame.size.height/2);
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.frame = CGRectMake(0,frame.size.height/3,frame.size.width,frame.size.height/3);
    doneButton.frame = CGRectMake(frame.size.width - 154, frame.size.height - 90, 100, 34);
    tellButton.frame =CGRectMake(54, frame.size.height - 90, 100, 34);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top