1
votes

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.

2

2 Answers

1
votes

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

0
votes

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