[英]Hide bottom table view when orientation changed to landscape mode in Swift ios
我有一个UIView和一个UITableView在纵向模式下以相等的高度共享屏幕。
现在,当方向更改为横向模式时,如何隐藏表格视图并用UIView填充整个屏幕。 并以纵向模式还原UITableView和UIView。
提前致谢。
我建议您在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> device rotaion中设置设备旋转
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 } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.