Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios7 为什么';当我显示景观时,是否不能在景观中使用视图控制器?_Ios7_Xcode5_Autolayout_Uistoryboard_Landscape Portrait - Fatal编程技术网

Ios7 为什么';当我显示景观时,是否不能在景观中使用视图控制器?

Ios7 为什么';当我显示景观时,是否不能在景观中使用视图控制器?,ios7,xcode5,autolayout,uistoryboard,landscape-portrait,Ios7,Xcode5,Autolayout,Uistoryboard,Landscape Portrait,我通过以下代码在主屏幕上添加UIViewController: SettingViewController *v=[ self.storyboard instantiateViewControllerWithIdentifier: @"SettingViewController"]; [self addChildViewController:v]; [self.view addSubview:v.view]; [v didMoveToParentViewController:self]; 我

我通过以下代码在主屏幕上添加UIViewController:

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

[self addChildViewController:v];
[self.view addSubview:v.view];
[v didMoveToParentViewController:self];
我的问题是: 显示的视图在“景观”中不起作用!!为什么


我找到了一个解决方案,在显示UIViewController之前以及在更改设备方向时更改UIViewController的框架,如下所示:

//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);

您可以在视图控制器上以编程方式添加约束(设置VC):

// 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];