【发布时间】:2015-09-08 07:54:26
【问题描述】:
我有一个 UIView 和一个 UITableView 在纵向模式下共享相同高度的屏幕。
现在,当方向更改为横向模式时,如何隐藏表格视图并用 UIView 填充整个屏幕。并以纵向模式恢复 UITableView 和 UIView。
提前致谢。
【问题讨论】:
标签: ios swift orientation nslayoutconstraint
我有一个 UIView 和一个 UITableView 在纵向模式下共享相同高度的屏幕。
现在,当方向更改为横向模式时,如何隐藏表格视图并用 UIView 填充整个屏幕。并以纵向模式恢复 UITableView 和 UIView。
提前致谢。
【问题讨论】:
标签: ios swift orientation nslayoutconstraint
我建议你在 ViewController 中实现 viewWillTransitionToSize
var landscapeViewFrame = CGRect()
var landscapeTableViewFrame = CGRect()
var portraitViewFrame = CGRect()
var portraitTableViewFrame = CGRect()
override func viewDidLoad() {
super.viewDidLoad()
landscapeViewFrame = CGRectMake(0, 0, view.frame.width / 2, view.frame.height)
landscapeTableViewFrame = CGRectMake(view.frame.width, 0, view.frame.width / 2, self.view.frame.height);
portraitViewFrame = view.bounds
portraitTableViewFrame = CGRectMake(0,0,0,0);
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
let isPortrait = (size.height > size.width);
yourView.frame = isPortrait ? portraitViewFrame : landscapeViewFrame
yourTableView.frame = isPortrait ? portraitTableViewFrame : landscapeTableViewFrame
yourTableView.hidden = isPortrait
}
viewWillTransitionToSize 通知您的 viewController 视图框架将发生变化。 UIViewControllerTransitionCoordinator 包含有关动画的信息,例如持续时间/如果有动画。
更新现在迅速。
基本思路:先计算指定帧。在设备旋转更改时,会调用 viewWillTransitionToSize 并可以设置视图的框架。如前所述,您可能需要动画来使一切看起来流畅
【讨论】:
1.首先在yourProject中设置设备旋转>General>设备旋转
2 写在你的视图中会出现
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)` name:UIDeviceOrientationDidChangeNotification object:nil];
将这些方法写入您想要的框架或要隐藏或取消隐藏的视图
- (void)orientationChanged:(NSNotification *)notification
{
[self handleOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
- (void) handleOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//handle the portrait view
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//handle the landscape view
}
}
【讨论】: