当我展示它时,为什么不在景观中使用视图控制器?

时间:2014-08-18 07:20:54

标签: ios7 xcode5 autolayout uistoryboard landscape-portrait

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

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

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

我的问题: 呈现的视图在景观中不起作用!!为什么?

enter image description here

2 个答案:

答案 0 :(得分:1)

我在显示它之前通过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);

答案 1 :(得分:1)

您可以在视图控制器上以编程方式添加约束(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];