简体   繁体   中英

Hide bottom table view when orientation changed to landscape mode in Swift ios

I have one UIView and one UITableView sharing the screen with equal height in portrait mode.

Now how do I hide table view and fill entire screen with UIView when orientation is changed to landscape mode. and restore both UITableView and UIView in portrait mode.

Thanks in advance.

I suggest you implement viewWillTransitionToSize in your ViewController

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 notifies your viewController that the view frame is going to change. The UIViewControllerTransitionCoordinator contains information about the animation, such as the duration / if animated at all.

Update now in swift. Basic idea: calculate the designated frames before. On device rotation change, the viewWillTransitionToSize is called and the frames for your views can be set. As mentioned before, you'll probably need animations to make everything look smooth

1.first set device rotation in yourProject>General>device rotaion

2 write in your viewwillappear

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)`  name:UIDeviceOrientationDidChangeNotification  object:nil];
  1. write these method to what frame you want or which view you want to hide or unhide

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM