Skip to content

Instantly share code, notes, and snippets.

View IsaAliev's full-sized avatar

Isa Aliev IsaAliev

  • Moscow, Russia
View GitHub Profile
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
struct ModalView: View {
let dismiss: () -> Void
var body: some View {
VStack {
Text("This is modal view")
.padding()
Button("Close") {
withAnimation {
dismiss()
// 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
struct AccountView: View {
@State var showsModal: Bool = false
var body: some View {
ZStack {
Button("Show modal") {
withAnimation {
showsModal = true
}
}
struct MainView: View {
var body: some View {
TabView() {
AccountView()
.tabItem {
Image(systemName: "person.crop.circle")
}
Text("Second")
.tabItem {
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
}
class TestCell: UICollectionViewCell, BoundingWidthAdoptable {
private var widthConstraint: Constraint?
func adoptBoundingWidth(_ width: CGFloat) {
widthConstraint?.update(offset: width)
}
...
}
contentView.snp.makeConstraints { make in
make.edges.equalToSuperview()
widthConstraint = make.width.equalTo(100).constraint
}
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)
protocol BoundingWidthAdoptable {
func adoptBoundingWidth(_ width: CGFloat)
}