当设备处于横向模式而不是纵向模式时,我试图让滚动视图占据整个屏幕这是我在纵向模式下的视图。我希望这能像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
最佳答案
是否对此视图使用自动布局?如果是这样,您可以在方向改变时根据需要更改scrollview的常量(您可以使用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];
}
关于ios - 横向时全屏快速滚动查看,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44094752/