swift 横屏时滚动查看全屏

swift scroll view fullscreen when landscape

当设备处于横向而非纵向时,我试图让滚动视图占据整个屏幕这是我在纵向模式下的视图。我希望它能像 youtube 应用程序处理视频一样工作。任何示例代码都会非常有帮助

这就是我设置滚动视图的方式

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

您是否为此视图使用了自动版式?如果是这样,您可以在方向更改时根据需要更改滚动视图的约束(您可以使用UIDeviceOrientationDidChangeNotification),或者如果您只是在代码中设置框架,请相应地进行框架调整

在您的视图中注册通知将出现

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

并且此方法将在方向更改时被调用

-

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

}

别忘了移除观察者

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