Question

I have an iPhone app, It should support both landscape and portrait orientation.I have set Use Autolayout as NO.

In portrait orientation , my view appears as enter image description here

In landscape orientation , my view appears as enter image description here

I have set the autosizing for both the views like the below image

enter image description here

Why the distance between the views are increasing on landscape mode? Can anyone help me out?

Était-ce utile?

La solution

Someone can correct me if I am wrong but it seems like this is the correct behavior to me. The views are not resizing and are not fixed to any one side of your superview. The superview is just getting wider, and so the area between them becomes bigger. If you want to set them at a certain location and dont want to use autolayout then the best way is just to set the frames where you want them when a rotation occurs. You can do this in two steps: First set up your frames- i like to define a nice structure as follows

//Use this define to set frames for views
#define TAG_RECT( tag, x, y, width, height ) \
[NSValue valueWithCGRect:CGRectMake(x, y, width, height)], \
[NSNumber numberWithInteger:tag]

You then set up your frames in your viewDidLoad: A good way to know where to place your view is by copying your view controller in storyboard and paste it to the side. then you can change it to landscape orientation and place the views exactly where you want them and just look at what the position and dimensions are. dont forget to set your tags for your views.

// Collect the frame positions for elements in portrait mode
    NSMutableDictionary *portraitPositions = [[NSMutableDictionary alloc] init];
// You only have two views, but if you have more its nice to do it in a loop
    for (NSInteger i = 1; i <= 2; i++) {
        UIView *view = [self.view viewWithTag:i];

        [portraitPositions setObject:[NSValue valueWithCGRect:view.frame] forKey:[NSNumber numberWithInteger:i]];
    }
    self.portraitFrames = [portraitPositions copy];

    // Let's build the landscape frame positions dictionary
    if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) {
        //Set up frames for variables in iPad version
        self.landscapeFrames = [NSDictionary dictionaryWithObjectsAndKeys:
                                TAG_RECT(1, 325, 100, 375, 90),     // view one
                                TAG_RECT(2, 525, 100, 375, 90),     // view two 
                                nil];
    }

Next whenever your view rotates you need to layout the cooresponding frame set

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {    
        // Lay out for landscape mode
        [self layoutForFrameSet:self.landscapeFrames];
    }
    else if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        // Lay out for portrait mode
        [self layoutForFrameSet:self.portraitFrames];
    }
}


- (void)layoutForFrameSet:(NSDictionary *)frames {
    for (NSNumber *key in frames.allKeys) {
        [self.view viewWithTag:[key integerValue]].frame = [[frames objectForKey:key] CGRectValue];
    }
}

Sorry if this is more complicated but this is the best way to have your views be placed exactly where you want them without autolayout

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top