繁体   English   中英

方向改变时如何设置框架?

[英]How to set frame if orientation change?

任何人都可以给我提示如何在方向改变时为滚动视图设置框架吗?对,这就是我要显示图像的方式。任何帮助都会对您有所帮助。

  mainScrollView.frame = self.view.frame

    for i in 0 ..< images.count {

        let imageView = UIImageView()
        imageView.image = images[i].image
        imageView.contentMode = .scaleAspectFit
        let xPosition = self.view.frame.width * CGFloat(i)
        imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
        mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
        mainScrollView.addSubview(imageView)

    }
    if let index = images.index(where: { $0.imageName == selectedImageName }){
        let position = CGPoint(x: (mainScrollView.frame.width * CGFloat(index)), y: 0)
        mainScrollView.setContentOffset(position, animated: true)
    }

VDL方法中触发此通知

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

并实现以下方法

func rotated() {
    if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation))
    {
        print("landscape")
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation))
    {
        print("Portrait")
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM