-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Production ready code for UIKit in SwiftUI (#6)
* First iteration * Second iteration * Iteration 3 * Start of iteration 4 * Create IntrinsicContentView * Fixed content size * Update readme, clean up code
- Loading branch information
Showing
10 changed files
with
252 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// IntrinsicContentView.swift | ||
// | ||
// | ||
// Created by Antoine van der Lee on 23/07/2022. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
public final class IntrinsicContentView<ContentView: UIView>: UIView { | ||
let contentView: ContentView | ||
let layout: Layout | ||
|
||
init(contentView: ContentView, layout: Layout) { | ||
self.contentView = contentView | ||
self.layout = layout | ||
|
||
super.init(frame: .zero) | ||
backgroundColor = .clear | ||
addSubview(contentView) | ||
clipsToBounds = true | ||
} | ||
|
||
@available(*, unavailable) required init?(coder _: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private var contentSize: CGSize = .zero { | ||
didSet { | ||
invalidateIntrinsicContentSize() | ||
} | ||
} | ||
|
||
public override var intrinsicContentSize: CGSize { | ||
switch layout { | ||
case .intrinsic: | ||
return contentSize | ||
case .fixedWidth(let width): | ||
return .init(width: width, height: contentSize.height) | ||
case .fixed(let size): | ||
return size | ||
} | ||
} | ||
|
||
public func updateContentSize() { | ||
switch layout { | ||
case .fixedWidth(let width): | ||
// Set the frame of the cell, so that the layout can be updated. | ||
var newFrame = contentView.frame | ||
newFrame.size = CGSize(width: width, height: UIView.layoutFittingExpandedSize.height) | ||
contentView.frame = newFrame | ||
|
||
// Make sure the contents of the cell have the correct layout. | ||
contentView.setNeedsLayout() | ||
contentView.layoutIfNeeded() | ||
|
||
// Get the size of the cell | ||
let computedSize = contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) | ||
|
||
// Apple: "Only consider the height for cells, because the contentView isn't anchored correctly sometimes." We use ceil to make sure we get rounded numbers and no half pixels. | ||
contentSize = CGSize(width: width, height: ceil(computedSize.height)) | ||
case .fixed(let size): | ||
contentSize = size | ||
case .intrinsic: | ||
contentSize = contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// ModifiedUIViewContainer.swift | ||
// | ||
// | ||
// Created by Antoine van der Lee on 23/07/2022. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
import UIKit | ||
|
||
public struct ModifiedUIViewContainer<ChildContainer: UIViewContaining, Child, Value>: UIViewContaining where ChildContainer.Child == Child { | ||
|
||
let child: ChildContainer | ||
let keyPath: ReferenceWritableKeyPath<Child, Value> | ||
let value: Value | ||
|
||
public func makeCoordinator() -> UIViewContainingCoordinator<Child> { | ||
child.makeCoordinator() as! UIViewContainingCoordinator<Child> | ||
} | ||
|
||
public func makeUIView(context: Context) -> IntrinsicContentView<Child> { | ||
context.coordinator.createView() | ||
} | ||
|
||
public func updateUIView(_ uiView: IntrinsicContentView<Child>, context: Context) { | ||
update(uiView.contentView, coordinator: context.coordinator, updateContentSize: true) | ||
} | ||
|
||
public func update(_ uiView: Child, coordinator: UIViewContainingCoordinator<Child>, updateContentSize: Bool) { | ||
uiView[keyPath: keyPath] = value | ||
child.update(uiView, coordinator: coordinator, updateContentSize: false) | ||
|
||
if updateContentSize { | ||
coordinator.view?.updateContentSize() | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.