AbhiRam
AbhiRam

Reputation: 2061

How to apply constraint programmatically based on simulator orientations

In my ViewController I have added one UIView programmatically using Auto-layouts

Here I am applying constraints based on simulator orientations but here based on my code constraints only applying when simulator at portrait mode but when change it at landscape mode constraints are not applying what did I do here wrong

my code:

#import "MainViewController.h"

@interface MainViewController (){

    UIView * MainView;
}

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    MainView = [[UIView alloc] init];
    MainView.backgroundColor = [UIColor orangeColor];
    MainView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:MainView];

    [self AppyingAutolayoutsAtPortraint];
}

-(void)AppyingAutolayoutsAtPortraint{

    NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(MainView);

    //Appliying Autolayouts for MainView

    [self.view removeConstraints:self.view.constraints];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[MainView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[MainView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

     [self.view setNeedsLayout];

}

-(void)AppyingAutolayoutsLandAtScpae{

    NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(MainView);

    //Appliying Autolayouts for MainView

    [self.view removeConstraints:self.view.constraints];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-10-[MainView]-10-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-10-[MainView]-10-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

     [self.view setNeedsLayout];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context){

         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        if (orientation == UIInterfaceOrientationPortrait) {

            NSLog(@"UIInterfaceOrientationPortrait");
             [self AppyingAutolayoutsAtPortraint];
        }
        else if (orientation == UIInterfaceOrientationLandscapeLeft) {


            NSLog(@"UIInterfaceOrientationLandscapeLeft");
             [self AppyingAutolayoutsLandAtScpae];

        }else if (orientation == UIInterfaceOrientationLandscapeRight){

              NSLog(@"UIInterfaceOrientationLandscapeRight");
              [self AppyingAutolayoutsLandAtScpae];
        }

     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

     }];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

@end

Upvotes: 0

Views: 81

Answers (2)

Harikrishnan
Harikrishnan

Reputation: 8065

You can use size classes for your requirement. Using size classes, when designing your app, you can now create a single layout, which works on all current iOS devices – without crufty platform-specific code. All you need to do is to open storyboard, goto file inspector and make sure that the Use Size Classes checkbox is ticked.

Here is a really nice tutorial on Size Classes.

Upvotes: 2

J&#246;rn Buitink
J&#246;rn Buitink

Reputation: 2916

You call 2 different functions in your UIInterfaceOrientationLandscapeLeft and UIInterfaceOrientationLandscapeRight

This seems to make no sense:

    else if (orientation == UIInterfaceOrientationLandscapeRight){

          NSLog(@"UIInterfaceOrientationLandscapeRight");
         [self AppyingAutolayoutsAtPortraint]; //?
    }

Upvotes: 0

Related Questions