【问题标题】:Why doesn't work a view controller at landscape when I show it?为什么当我显示视图控制器时它不能在景观中工作?
【发布时间】:2014-10-11 01:26:09
【问题描述】:
我通过这段代码在主屏幕上添加 UIViewController :
SettingViewController *v=[ self.storyboard instantiateViewControllerWithIdentifier: @"SettingViewController"];
[self addChildViewController:v];
[self.view addSubview:v.view];
[v didMoveToParentViewController:self];
我的问题:
呈现的视图在横向上不起作用!!为什么??
【问题讨论】:
标签:
ios7
xcode5
autolayout
uistoryboard
landscape-portrait
【解决方案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];
【解决方案2】:
我找到了一个解决方案,通过在显示之前更改 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);