I know Auto Layout can be used to make the sizes and position consistent when the orientation changes. Is it possible to completely change the layout when the orientation change
It can be done with layout constraints something like I have below. This doesn't quite work, because your sketch doesn't quite work -- your landscape view is too long compared with what you really get in landscape. You would have to shorten the login text fields for everything to fit, but this should give you an idea how it's done. The buttonWidth constraint shows how to have a negative correlation between the width of the view and the width of a button -- that is, the button's width will be less in landscape than in portrait. I have several IBOutlets to constraints that I reference in the code. I can describe them for you if you're interested, but I'll just throw this out there for now:
@implementation ViewController {
IBOutlet NSLayoutConstraint *buttonWidth;
IBOutlet NSLayoutConstraint *logoTop;
IBOutlet NSLayoutConstraint *logoAlignToLabel;
IBOutlet NSLayoutConstraint *logoSpaceToLabel;
IBOutlet NSLayoutConstraint *coNameToButtonAlignment;
IBOutlet UIButton *b;
IBOutlet UIImageView *logo;
IBOutlet UILabel *coName;
NSLayoutConstraint *con2;
NSArray *cons1;
}
- (void)viewDidLoad {
[super viewDidLoad];
[b removeConstraint:buttonWidth];
buttonWidth = [NSLayoutConstraint constraintWithItem:b attribute:NSLayoutAttributeWidth relatedBy:0 toItem:self.view attribute:NSLayoutAttributeWidth multiplier:-.2193 constant:350];
[self.view addConstraint:buttonWidth];
[self.view layoutSubviews];
}
- (void)updateViewConstraints{
[super updateViewConstraints];
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
if (con2 != nil) {
[self.view removeConstraints:cons1];
[self.view removeConstraint:con2];
[self.view addConstraints:@[logoAlignToLabel,logoSpaceToLabel,logoTop,coNameToButtonAlignment]];
}
}else{
NSLog(@"Landscape");
[self.view removeConstraints:@[logoAlignToLabel,logoSpaceToLabel,logoTop,coNameToButtonAlignment]];
cons1 = [NSLayoutConstraint constraintsWithVisualFormat:@"|-8-[logo]-4-[coName]" options:NSLayoutFormatAlignAllCenterY metrics:0 views:@{@"logo":logo, @"coName":coName}];
con2 = [NSLayoutConstraint constraintWithItem:logo attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[self.view addConstraints:cons1];
[self.view addConstraint:con2];
}
}
On rotation the logo and company name label have their constraints removed, and new ones put in place. The new constraint for the button, that I put on in viewDidLoad, takes care of rotation automatically, so I don't have to adjust it at all during the rotation.