我正在创建一个使用KVO观察UITableView的contentOffset的UIView子类。我正在使用contentOffset来调整UIView子类中子视图的大小。 automaticAdjustsScrollViewInsets会导致scrollView.contentOffset为-20,这很好,但是在我的UIView子类的layoutSubviews中,scrollView.contentOffset为0。这是我在此处设置属性“ initialContentOffset”的地方。直到稍后它才被设置为-20。

发生此更改时,有没有办法通知您?换句话说,我需要能够在父视图(或任何设置它的人)进行此调整后设置属性“ initalContentOffset”。

我已经检查了scrollView的contentInset属性,它全都是0。

下面是代码,这是下载项目的链接:
https://www.dropbox.com/s/0tvv33pcrikrayz/ContentOffsetTest.zip?dl=0

ViewController.swift

import UIKit

class ViewController: UIViewController {

@IBOutlet var tableView: UITableView!
@IBOutlet var someView: UIView!



override func viewDidLoad() {
    super.viewDidLoad()

    let aView = SomeView()
    aView.scrollView = self.tableView
    self.someView = aView

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 60

 }
 }

extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 100
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = String(describing: indexPath.row)
    return cell
}
}


SomeView.swift

import UIKit

private var ContentOffsetKVO = 0
private var ContentSizeKVO = 0

class SomeView: UIView {

var view: UIView!

public var scrollView: UIScrollView? {
    didSet {
        if let view = oldValue {
            removeKVO(scrollView: view)
        }

        if let view = scrollView {
            addKVO(scrollView: view)
            updateScrollPosition()
        }
    }
}

func xibSetup() {
    view = loadFromNib()
    view.frame = bounds
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    addSubview(view)
}

override func layoutSubviews() {
    super.layoutSubviews()
}

func loadFromNib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "SomeView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

    return view
}


override init(frame: CGRect) {
    // 1. setup any properties here

    // 2. call super.init(frame:)
    super.init(frame: frame)
    // 3. Setup view from .xib file
    xibSetup()

}

required init?(coder aDecoder: NSCoder) {
    // 1. setup any properties here

    // 2. call super.init(coder:)
    super.init(coder: aDecoder)
    // 3. Setup view from .xib file
    xibSetup()
}




deinit {
    if let scrollView = scrollView {
        removeKVO(scrollView: scrollView)
    }
}

// MARK: - KVO
private func removeKVO(scrollView: UIScrollView) {
    scrollView.removeObserver(
        self,
        forKeyPath: "contentSize",
        context: &ContentSizeKVO
    )
    scrollView.removeObserver(
        self,
        forKeyPath: "contentOffset",
        context: &ContentOffsetKVO
    )
}

func updateScrollPosition() {
    let offsetY: CGFloat = scrollView!.contentOffset.y

    print("updateScrollPosition (offsetY: \(offsetY))")
}

private func addKVO(scrollView: UIScrollView) {
    scrollView.addObserver(
        self,
        forKeyPath: "contentSize",
        options: [.initial, .new],
        context: &ContentSizeKVO
    )
    scrollView.addObserver(
        self,
        forKeyPath: "contentOffset",
        options: [.initial, .new],
        context: &ContentOffsetKVO
    )
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    switch keyPath {

    case .some("contentSize"), .some("contentOffset"):
            self.updateScrollPosition()

    default:
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }

     }

}

最佳答案

看来它毕竟是在scrollView.contentInset中设置的-只是在稍后的时间。

 (lldb) po self.scrollView?.contentInset
▿ Optional<UIEdgeInsets>
  ▿ some : UIEdgeInsets
 - top : 20.0
- left : 0.0
- bottom : 0.0
- right : 0.0

关于ios - Swift中自动将AdjustsScrollViewInsets结果存储在哪里?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43682680/

10-12 21:35