首页 > 解决方案 > UIScrollView 内容插图无法移除?

问题描述

您好,我正在尝试使用 swift 创建一个侧滚动条(我需要与 IOS9 兼容,但我无法使用UIScrollview.contentInsetAdjustmentBehavior = .never),并且在创建没有内容插入的 UIScrollView 时遇到问题。下面的代码显示了我所做的,我希望有人能指出我的一些东西。

override func viewDidLoad() {
        super.viewDidLoad()

        automaticallyAdjustsScrollViewInsets = false // 

        let someView = UIView()
        someView.frame = UIScreen.main.bounds
        someView.backgroundColor = .yellow
        view.addSubview(someView)

        // some image size calcs to keep the image in the same proportions on all devices.
        let frame = view.frame
        let height = frame.height * 0.35
        let ratioFactor = CGFloat(8.82)
        let width = height * ratioFactor

        let hillsImgView = UIImageView()
        let image = UIImage(named: "hills")
        hillsImgView.image = image

        // place imageView at the bottom of the screen
        let rect = CGRect(x: 0, y: frame.height - height, width: width, height: height)
        hillsImgView.frame = rect
        hillsImgView.backgroundColor = .green
        hillsImgView.contentMode = UIImageView.ContentMode.scaleToFill

        let hillsScrollView = UIScrollView()
        hillsScrollView.frame = UIScreen.main.bounds
        hillsScrollView.frame.origin.x = 10 // just to see the parent views yellow bg
        hillsScrollView.backgroundColor = .red
        hillsScrollView.contentSize = hillsImgView.frame.size // the image is longer then the screen
        hillsScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0 right: 0)
        hillsScrollView.contentOffset = CGPoint(x: 0, y: 0)
        hillsScrollView.addSubview(hillsImgView)

        someView.addSubview(hillsScrollView)
    }

我可以设置 imageview x 位置来移除 SafeArea 插图,但我觉得这也不是一个很好的解决方案。

干杯 T。

模拟器结果

标签: iosswiftuiscrollviewinsets

解决方案


推荐阅读