簡體   English   中英

在Swift ios中將方向更改為橫向模式時隱藏底表視圖

[英]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];
  1. 將這些方法寫到所需的幀或要隱藏或取消隱藏的視圖

     - (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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM