Skip to content

Instantly share code, notes, and snippets.

@isoiphone
Created July 19, 2016 22:38
Show Gist options
  • Save isoiphone/deebeb04d6c56578888a7a88fbe0afbf to your computer and use it in GitHub Desktop.
Save isoiphone/deebeb04d6c56578888a7a88fbe0afbf to your computer and use it in GitHub Desktop.
readableContentGuide is a mess.
class ReadableContentGuideHelperView: UIView {
var onChange: ((CGRect)->())?
static func addToView(view: UIView) -> ReadableContentGuideHelperView {
let helperView = ReadableContentGuideHelperView()
view.addSubview(helperView)
if #available(iOS 9.0, *) {
helperView.setupAnchors(view)
} else {
helperView.setupFrame(view)
}
return helperView
}
@available(iOS 9.0, *)
private func setupAnchors(view: UIView) {
translatesAutoresizingMaskIntoConstraints = false
leadingAnchor.constraintEqualToAnchor(view.readableContentGuide.leadingAnchor).active = true
trailingAnchor.constraintEqualToAnchor(view.readableContentGuide.trailingAnchor).active = true
topAnchor.constraintEqualToAnchor(view.readableContentGuide.topAnchor).active = true
bottomAnchor.constraintEqualToAnchor(view.readableContentGuide.bottomAnchor).active = true
}
private func setupFrame(view: UIView) {
translatesAutoresizingMaskIntoConstraints = true
autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
frame = view.bounds
}
init() {
readableContentFrame = CGRect.zero
super.init(frame: CGRect.zero)
hidden = true
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
private (set) var readableContentFrame: CGRect
override func layoutSubviews() {
super.layoutSubviews()
guard frame != readableContentFrame else { return }
readableContentFrame = frame
log.debug("ReadableContentGuideHelperView new frame: \(frame)")
onChange?(frame)
}
}
@isoiphone
Copy link
Author

Maybe this fugly thing works with KVO?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment