Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion UIImageViewAligned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,19 @@ open class UIImageViewAligned: UIImageView {
}

private func setup(image: UIImage? = nil, highlightedImage: UIImage? = nil) {
addRotationObserver()
realImageView = UIImageView(image: image ?? super.image, highlightedImage: highlightedImage ?? super.highlightedImage)
realImageView?.frame = bounds
realImageView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
realImageView?.contentMode = contentMode
addSubview(realImageView!)
}

private func updateLayout() {
deinit {
NotificationCenter.default.removeObserver(self)
}

@objc public func updateLayout() {
let realSize = realContentSize
var realFrame = CGRect(origin: CGPoint(x: (bounds.size.width - realSize.width) / 2.0,
y: (bounds.size.height - realSize.height) / 2.0),
Expand Down Expand Up @@ -254,4 +259,9 @@ open class UIImageViewAligned: UIImageView {
private func getInspectableProperty(_ alignment: UIImageViewAlignmentMask) -> Bool {
return self.alignment.contains(alignment)
}

private func addRotationObserver() {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(updateLayout), name: UIDevice.orientationDidChangeNotification, object: nil)
}
}