カスタムアライメントガイド
階層をまたいでビューを正確に揃えます。
「カスタムアライメントガイド」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Lining Things Up
Stacks align children using a shared edge or center. Alignment guides let you fine-tune exactly where that line falls. 📏
Built-in Alignments
An HStack takes a vertical alignment like .top or .center, while a VStack takes a horizontal one.
HStack(alignment: .top) {
Text("A")
Text("Big")
}The alignmentGuide Modifier
The alignmentGuide modifier overrides where a single view exposes a given alignment to its parent.
Text("Hi")
.alignmentGuide(.top) { d in d[.bottom] }Reading Dimensions
The closure gets a ViewDimensions value, letting you read d.width, d.height, or any named guide.
Text("Hi").alignmentGuide(.leading) { d in
d.width / 2
}Shift the Guide
Returning a different number moves the view relative to its peers, since the parent lines up everyone on that returned value.
Custom Alignment ID
For reuse, define a type conforming to AlignmentID with a defaultValue for unaligned cases.
struct MidBadge: AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
d[.bottom]
}
}Name It on Alignment
Extend VerticalAlignment or HorizontalAlignment with a static property bound to your AlignmentID.
extension VerticalAlignment {
static let midBadge = VerticalAlignment(MidBadge.self)
}Use the Custom Line
Pass your named alignment to a stack, then tag the views that should snap to that custom line.
HStack(alignment: .midBadge) {
Avatar()
Label()
}Tag the Participants
Each view that should share the line sets alignmentGuide for your custom alignment to the point it wants to match.
Label()
.alignmentGuide(.midBadge) { d in
d[VerticalAlignment.center]
}Cross-Hierarchy Power
Custom guides shine when views live in different containers but must still align, like a caption to an icon center across columns. ✨
Mind the Default
Any view that does not set your custom guide falls back to its defaultValue, so pick that default thoughtfully.
Quick Check
Recall what the alignmentGuide closure receives.
Recap
You can now override alignment with alignmentGuide and define custom AlignmentID types to align views across the hierarchy. 🎉
よくある質問
「カスタムアライメントガイド」レッスンは無料ですか?
はい。「カスタムアライメントガイド」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「カスタムアライメントガイド」で何を学びますか?
階層をまたいでビューを正確に揃えます。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「カスタムアライメントガイド」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- GeometryReaderの基本
- カスタムアライメントガイド
- Layoutプロトコル
- フローレイアウトを作る