To achieve the desired layout where profile buttons scroll horizontally and the add button remains fixed on the right, you'll need to adjust the structure of your ProfileSegmentView. The main issue arises because the ScrollView is taking up the entire width of the screen, which can cause layout problems, especially when there's only one profile.
Here's how you can modify your ProfileSegmentView to ensure that the profile buttons can scroll horizontally while keeping the add button fixed on the right:
-
Structure the HStack Properly:
- Use an
HStack to arrange the ScrollView and the add button side by side.
- Ensure that the
ScrollView takes up the available space while the add button maintains a fixed size.
-
Embed Profiles in an Inner HStack:
- Inside the
ScrollView, use an HStack to lay out the profile buttons horizontally.
- This allows the profiles to scroll smoothly when there are multiple items.
-
Set Appropriate Frame Constraints:
- Use
.frame(maxWidth: .infinity, alignment: .leading) on the ScrollView to make it expand and take up available space without overlapping the add button.
- Assign a fixed size to the add button to ensure it remains visible and properly aligned.
-
Add Padding and Styling:
- Apply padding and background styling to enhance the visual appearance and ensure consistent spacing.
Here's the revised code implementing these changes:
struct ProfileSegmentView: View {
let profiles: [Profile]
let onProfile: (Profile) -> Void
let onAdd: () -> Void
var body: some View {
HStack(spacing: 6) {
// Scrollable profile buttons
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 6) { // Inner HStack for horizontal layout
ForEach(profiles) { profile in
Button(action: {
onProfile(profile)
}) {
Text(profile.name)
.typography(.H4.regular)
.foregroundColor(profile.isCurrent ? .white : .palette.txSecondary)
.padding(.vertical, 4)
.padding(.horizontal, 16)
.frame(height: 34)
.background(
Color.white.opacity(profile.isCurrent ? 0.1 : 0)
.roundedCorners()
)
}
.buttonStyle(.plain)
}
}
.padding(.leading, 8) // Optional padding for better spacing
}
.frame(maxWidth: .infinity, alignment: .leading) // Allow ScrollView to take available space
// Fixed add button
Button(action: onAdd) {
Image(systemName: "plus") // Use system image for better compatibility
.resizable()
.scaledToFit()
.frame(width: 14, height: 14)
.padding()
.background(
Circle()
.fill(Color.white.opacity(0.07))
.frame(width: 34, height: 34)
)
}
.frame(width: 34, height: 34)
.padding(.horizontal, 8)
.buttonStyle(.plain)
}
.frame(height: 50)
.padding(.horizontal, 8)
.background(
Color.palette.primary900
.opacity(0.4)
.background(BackdropBlurView(radius: 8)) // Assuming this is a custom blur view
.roundedCorners()
)
}
}
Key Changes and Explanations:
-
Inner HStack within ScrollView:
- By embedding the
ForEach loop inside an inner HStack, you ensure that profile buttons are laid out horizontally and can scroll as needed.
-
ScrollView Frame Adjustment:
- The
.frame(maxWidth: .infinity, alignment: .leading) ensures that the ScrollView takes up all available horizontal space except what’s needed by the add button. This allows the profiles to expand and scroll without pushing the add button off-screen.
-
Fixed Add Button:
- The add button is given a fixed size (
34x34) and padding to maintain its position on the right. It remains visible regardless of the number of profiles.
-
Styling Enhancements:
- Replaced
Image(.plus) with Image(systemName: "plus") for better compatibility and to use SF Symbols.
- Ensured consistent padding and frame sizes for a uniform appearance.
- Applied background styling with opacity and rounded corners to match the desired design.
-
Optional Padding:
- Added
.padding(.leading, 8) inside the ScrollView to provide spacing at the start of the scrolling content. This can be adjusted or removed based on design preferences.
-
Button Styles:
- Used
.buttonStyle(.plain) to remove any default button styling, ensuring that the custom styles are applied without interference.
Additional Considerations:
-
Dynamic Content:
- If you anticipate a varying number of profiles, this structure will handle both small and large numbers gracefully. The
ScrollView will enable horizontal scrolling when necessary.
-
Accessibility:
- Ensure that buttons are accessible by adding appropriate labels and traits if needed.
-
Performance:
- For a large number of profiles, consider using
LazyHStack inside the ScrollView to optimize performance.
Final Thoughts
By restructuring your HStack and properly constraining the ScrollView and add button, you ensure that the profile buttons scroll as intended while keeping the add button fixed on the right. This layout is flexible and maintains visual consistency across different content sizes.