如果方向改变,如何设置框架?

时间:2017-03-01 12:27:49

标签: ios swift

如果方向发生变化,有人可以给我提示如何为scrollview设置框架吗?现在我正在做什么来显示图像。任何帮助都会有帮助。

  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)
    }

1 个答案:

答案 0 :(得分:1)

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")
    }
}