Ryan Hubbard
Ryan Hubbard

Reputation: 247

swift scroll view fullscreen when landscape

I am trying to have a scroll view take up the whole screen when the device is in landscape but not in portrait This is my View in portrait mode. I want this to work the way the youtube app works with video. Any example code Would be very helpful

enter image description here

This is how I am setting up my scroll View

DispatchQueue.main.async(execute: { () -> Void in
            let imgURL = NSURL(string: self.property[i].image)
            let data = NSData(contentsOf: (imgURL as URL?)!)
            let imageView = UIImageView()
            imageView.image = UIImage(data: data! as Data)
            let xPosition = self.view.frame.width * CGFloat(i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)

            self.imageScrollView.contentSize.width = self.imageScrollView.frame.width * CGFloat(i + 1)
            self.imageScrollView.addSubview(imageView)
        }) //End DispatchQueue

Upvotes: 0

Views: 939

Answers (1)

Anshad Rasheed
Anshad Rasheed

Reputation: 2566

Are you using autolayout for this view? if so you can change the scrollview's constarints for your needs when orientation changes ( you can use UIDeviceOrientationDidChangeNotification) or if you are simply setting the frame in code do the frame adjustment accordingly

register notification in your viewwillappear

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)    name:UIDeviceOrientationDidChangeNotification  object:nil];

and this method will get called with orientation changes

-

 (void)orientationChanged:(NSNotification *)notification{

switch ([[UIApplication sharedApplication] statusBarOrientation])
    {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
        { 
        //set frame/ constarints for portrait    
        }

            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
        {
        //set frame/ constarints for landscape
        }
            break;
        case UIInterfaceOrientationUnknown:break;
    }

}

Don't forget to remove observer

-(void)viewDidDisappear:(BOOL)animated{
   [super viewDidDisappear:animated];
   [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
} 

Upvotes: 1

Related Questions