SwiftUI: Layout & Interfaces

Nov 18 2021 · Swift 5.5, iOS 15, Xcode 13

Part 2: Aligning Views

13. Custom Alignment

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 12. Alignment Guides Next episode: 14. Challenge: Custom Alignment Guides

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 13. Custom Alignment

This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

By now, you should be feeling pretty familiar with all of the built-in Alignments that SwiftUI offers to you. Three HorizontalAlignments and five VerticalAlignments.

        Text("Learn SwiftUI layout!")
          .alignmentGuide(HorizontalAlignment.center) { dimension in
            /*@START_MENU_TOKEN@*/ /*@PLACEHOLDER=Value@*/dimension[.top]/*@END_MENU_TOKEN@*/
          }
        Text("Learn SwiftUI layout!")
          .alignmentGuide(HorizontalAlignment.center) { $0[.leading] }
      }
VStack(alignment) {
}

enum CustomCenter: AlignmentID {

}

struct ScaledImage: View {
enum CustomCenter: AlignmentID {
  static func defaultValue(in context: ViewDimensions) -> CGFloat {
    <#code#>
  }
}
context[HorizontalAlignment.center]
extension HorizontalAlignment {
  enum CustomCenter: AlignmentID {
    static func defaultValue(in context: ViewDimensions) -> CGFloat {
      context[HorizontalAlignment.center]
    }
  }
}
  }

  static let customCenter =
}
static let customCenter = HorizontalAlignment(CustomCenter.self)
static let customCenter = Self(CustomCenter.self)
VStack(alignment: .customCenter) {
    static func defaultValue(in context: ViewDimensions) -> CGFloat {
//      context[HorizontalAlignment.center]
      -300
    }
context[HorizontalAlignment.center]
.alignmentGuide(.customCenter) { $0[.leading] }
        Text("Help others learn it too!")
          .alignmentGuide(.customCenter), computeValue: <#T##(ViewDimensions) -> CGFloat#>)
        ScaledImage("Hearts")
.alignmentGuide(.customCenter) { $0[.trailing] }