【问题标题】:swift scroll view fullscreen when landscape横向时快速滚动视图全屏
【发布时间】:2017-05-21 08:27:57
【问题描述】:

当设备处于横向而不是纵向时,我试图让滚动视图占据整个屏幕 这是我在纵向模式下的视图。我希望它以 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

【问题讨论】:

    标签: ios swift scroll landscape-portrait


    【解决方案1】:

    您是否为此视图使用自动布局?如果是这样,您可以在方向更改时根据需要更改滚动视图的约束(您可以使用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];
    } 
    

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      相关资源
      最近更新 更多