Make view fall in multiple rows like text in SwiftUI - swiftui

I have a similar problem to this: SwiftUI HStack with wrap and dynamic height
But I want this tags to aligned in center instead of left aligned. I tried but can't make it work.

You can try VStack with alignment.
VStack(alignment: .center) {
// your views
}

SkillChipView is just rename of TagCloudView.

Related

SwiftUI Remove Extra Space between NavigationTitle and TextView

Started learning SwiftUI, I want to remove this highlighted space between navigation title and Text inside VStack.
If you were to remove the NavigationView and had your VStack as the top-level view in your body, you'd see much the same layout.
That's because its frame is smaller than the frame available for it (in this case, most of the screen) and by default the smaller internal view gets centered both horizontally and vertically.
To move the text to the top of the screen, you need to ensure that your view will grow in size. The easiest way will be add to add a Spacer to the bottom of the VStack:
VStack(alignment: ...) {
Text("...")
Text("...")
Spacer()
}
Another approach would be to wrap your VStack in a scroll view. This view will automatically expand itself to fill the height, and layout its subviews (your VStack) from the top downwards:
ScrollView {
VStack(alignment: ...) {
Text("...")
Text("...")
}
}
Each approach has upsides and downsides depending on how much other data you expect to also be in the view.
You could also manually adjust the frame of your VStack using the .frame() modifier:
VStack(alignment: ...) {
Text("...")
Text("...")
}
.frame(maxHeight: .infinity, alignment: .top)
This would give you much the same effect as using the spacer. But the spacer version is a better way to go, especially if you're only starting out with SwiftUI layout.

How to overlay view on top of multiple other views in a VStack in SwiftUI

In SwiftUI I currently have a VStack of views, and I'd like to put an overlay over the bottom two views, like this:
VStack {
Spacer()
Group {
centerView
Spacer()
}
.overlay(overlayedView)
}
The goal of the above layout is ensure that centerView is vertically centered, while also ensuring that the overlayedView goes from the top of centerView all the way to the bottom of the VStack.
However, the above code actually results in one instance of overlayedView getting overlayed on top of centerView and another overlayedView getting overlayed on top of the bottom Spacer. But what I want is a single overlayedView spread on top of both centerView and the bottom Spacer.
How can I achieve my desired behavior?
Using Group will add view modifiers to all the subviews of the group, resulting in 2 seperate overlays. To have one common overlay, you can use VStack instead of Group.
VStack {
Spacer()
VStack {
centerView
Spacer()
}
.overlay(overlayedView)
}

SwiftUI TextEditor expands incorrectly in a VStack with maxWidth

I have two TextEditors in a VStack in my app. I would like each of them to expand to fit the text they contain, but remain hugging the text. Unfortunately, long text in the first TextEditor expands both TextEditors equally. It is as if the total height of the VStack they are in is expanded correctly, but the extra height is shared between the two editors.
This only occurs when I use a maxWidth on the VStack. If I fix the width it behaves correctly.
Is there some way to solve this without losing the resize behaviour maxWidth gives?
ScrollView {
VStack(alignment: .leading) {
TextEditor(text: Binding($note.details)!)
.font(.body)
TextEditor(text: Binding($note.title)!)
.font(.title)
}.frame(maxWidth: 450)
}
Try to put .fixedSize(horizontal: false, vertical: true) on both editors.

Prevent Navigation Bar from collapsing and being sticky when on scroll SwiftUI / UIKit

I would like the NavigationBar (from a SwiftUI NavigationView) to just stay large and scroll out whenever I scroll in a Pages ScrollView. Right now it is always collapsing and showing even on scroll in ".inline"-display mode. So when I try to completely hide it I also hide the large title I'd like to keep.
Is there a way to just let it stay large and just scroll out?
Any solution in SwiftUI or UIKit is appreciated as I can introspect from UIKit.
I especially don't want the NavigationBar to be hidden and rebuild by VStack or HStack as this causes loosing all Navigation Gestures like swiping back to the previous Navigation View and so...
Here is a basic codesnipped to recreate
struct TestTestView: View {
var body: some View {
NavigationView {
ScrollView() {
Color.green
.frame(height: 1000)
}
.navigationTitle("Test")
}
}
}

SwiftUI: Different Widths for Multiline Text Views in Boxes

Here is how two boxes with text are rendering in SwiftUI, but I want them to both be full-width:
Here is my code:
VStack(spacing: 20){
//Help ---
VStack(alignment: .leading, spacing:10){
Text("Help").h2()
Text("Please view our online user guide before contacting us since we answer most questions there.")
.lineLimit(nil) //Make the text wrap
.prefsText()
Button("Online Help"){
openURL(URL(string: "https://avid.pro/help")!)
}.foregroundColor(Color(accentColor))
.buttonStyle(PlainButtonStyle())
.prefsLink()
}.unoBoxRoundPad()
.frame(maxWidth: .infinity)
//Support ---
VStack(alignment: .leading, spacing:10){
Text("Support").h2()
Text("Email us if you need to submit a bug or get specialized help. It may take us a few days to get back to you.")
.lineLimit(nil) //Make the text wrap
.prefsText()
Button("Email Us"){
openURL(URL(string: "mailto:help#avid.pro")!)
}.foregroundColor(Color(accentColor))
.buttonStyle(PlainButtonStyle())
.prefsLink()
}.unoBoxRoundPad()
.frame(maxWidth: .infinity)
}.background(Color.yellow) //For testing
I can't for the life of me figure out why they aren't the same width. .unoBoxRoundPad() is a view modifier that adds these shared styles:
.padding(20)
.background(Color("UnoDark"))
.cornerRadius(7)
If I put .frame(maxWidth: .infinity) on the Text() instead of on the containing VStack, it seems to get a little closer, but then it disregards the containing view's padding():
Any ideas on what I'm doing wrong?
There are two issues at play here:
You've got two modifiers swapped (.frame(...) and .unoBoxRoundPad()); you want the roundedness to apply to the entire stretched thing. By putting .unoBoxRoundPad() first, you're saying "pad this thing" and then "place that rounded thing inside an infinitely-wide box"; you want the reverse: your thing should be placed inside an infinitely-wide box, and it's the infinitely wide box that should have the rounded corners and padding.
You need to specify an alignment: when using that .frame() modifier; when the inner view is placed inside an infinitely-wide box, it's going to default to being centered vertically and horizontally inside it. Based on your screenshots, you probably want to use .topLeading so that the content ("Support", "Help", etc) start in the top-left corner (or top-right in RTL languages).