1

I add UIViewController on main screen by this code :

SettingViewController *v=[ self.storyboard instantiateViewControllerWithIdentifier: @"SettingViewController"];

[self addChildViewController:v];
[self.view addSubview:v.view];
[v didMoveToParentViewController:self];

my problem: The presented view doesn't work at landscape!! Why??

enter image description here

Free User
  • 211
  • 2
  • 14

2 Answers2

1

I found a solution by change frame of UIViewController before show it and when change orientation of device as the following:

//Get Device Orientation.
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

// Add ViewController 
[self addChildViewController:settingVC];
[self.view addSubview:settingVC.view];
[settingVC didMoveToParentViewController:self];

// Change ViewController's Frame.   
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
     settingVC.view.frame=CGRectMake(0, 0, GetHeightOfDevice , GetWidthOfDevice);  
else
     settingVC.view.frame=CGRectMake(0, 0, GetWidthOfDevice, GetHeightOfDevice);
Free User
  • 211
  • 2
  • 14
1

You can add constraints programmatically on your view controller (settingVC):

// To Pure Auto Layout Approach
settingVC.view.translatesAutoresizingMaskIntoConstraints=NO;

// Add Constraints 
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:settingVC.view  attribute:NSLayoutAttributeLeading
                                                                                  relatedBy:0
                                                                                     toItem:self.view
                                                                                  attribute:NSLayoutAttributeLeft
                                                                                 multiplier:1.0
                                                                                   constant:0];
[self.view addConstraint:leftConstraint];

NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:settingVC.view
                                                                                   attribute:NSLayoutAttributeTrailing
                                                                                   relatedBy:0
                                                                                      toItem:self.view
                                                                                   attribute:NSLayoutAttributeRight
                                                                                  multiplier:1.0
                                                                                    constant:0];
[self.view addConstraint:rightConstraint];

NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:v.view  attribute:NSLayoutAttributeBottom
                                                                                  relatedBy:0
                                                                                     toItem:self.view
                                                                                  attribute:NSLayoutAttributeBottom
                                                                                 multiplier:1.0
                                                                                   constant:0];
[self.view addConstraint:bottomConstraint];

                NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:settingVC.view
                                                                                   attribute:NSLayoutAttributeTop
    relatedBy:0                                                                      attribute:NSLayoutAttributeTop
    multiplier:1.0
    constant:0];
    [self.view addConstraint:topConstraint];
Jameel
  • 1,126
  • 1
  • 14
  • 27