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
struct AccountView: View { | |
@State var showsModal: Bool = false | |
// 1 - Add EnvironmentObject variable to set your modal view | |
@EnvironmentObject var fullScreenModal: FullScreenModalProvider | |
var body: some View { | |
ZStack { | |
Button("Show modal") { | |
withAnimation { | |
// 2 - Set your modal view to content property |
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
struct ModalView: View { | |
let dismiss: () -> Void | |
var body: some View { | |
VStack { | |
Text("This is modal view") | |
.padding() | |
Button("Close") { | |
withAnimation { | |
dismiss() |
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
// 1 - Create class that will be used to pass new modal to root view | |
class FullScreenModalProvider: ObservableObject { | |
var content: AnyView? = nil { | |
willSet { objectWillChange.send() } | |
} | |
} | |
struct MainView: View { | |
// 2 - Add EnvironmentObject variable to root view | |
@EnvironmentObject var fullScreenModal: FullScreenModalProvider |
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
struct AccountView: View { | |
@State var showsModal: Bool = false | |
var body: some View { | |
ZStack { | |
Button("Show modal") { | |
withAnimation { | |
showsModal = true | |
} | |
} |
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
struct MainView: View { | |
var body: some View { | |
TabView() { | |
AccountView() | |
.tabItem { | |
Image(systemName: "person.crop.circle") | |
} | |
Text("Second") | |
.tabItem { |
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
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "id", for: indexPath) | |
cell.configure... | |
(cell as? BoundingWidthAdoptable)?.adoptBoundingWidth(collectionView.frame.width) | |
return cell | |
} |
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
class TestCell: UICollectionViewCell, BoundingWidthAdoptable { | |
private var widthConstraint: Constraint? | |
func adoptBoundingWidth(_ width: CGFloat) { | |
widthConstraint?.update(offset: width) | |
} | |
... | |
} |
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
contentView.snp.makeConstraints { make in | |
make.edges.equalToSuperview() | |
widthConstraint = make.width.equalTo(100).constraint | |
} |
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
class TestCell: UICollectionViewCell, BoundingWidthAdoptable { | |
private var boundingWidth: CGFloat = 0 | |
func adoptBoundingWidth(_ width: CGFloat) { | |
boundingWidth = width | |
} | |
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { | |
let attributes = super.preferredLayoutAttributesFitting(layoutAttributes) | |
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
protocol BoundingWidthAdoptable { | |
func adoptBoundingWidth(_ width: CGFloat) | |
} |
NewerOlder