I'm trying to align a view in SwiftUI the same way the new Workouts app for WatchOS 9 aligns with the time.
I have found this StackOverflow thread, but it does the exact opposite, I need alignment in the vertical axis.
So far I've tried the following, but it does not work for every screen size:
VStack(alignment: .leading) {
// my content here
}
.frame(maxWidth: .infinity, alignment: .leading)
.ignoresSafeArea(edges: [.bottom, .top]).scenePadding().focusable()
(red lines added by me to illustrate)
Related
I've got a simple Text element, here's the code:
Text("W-XYZ LAB IS CARBON NEUTRAL")
.foregroundColor(.white)
.frame(width: elementWidth, alignment: .leading)
.font(.system(size: 25, weight: .bold))
.padding(.bottom)
.border(.red)
elementWidth = screen width / 1.25
Take a look at the screenshot below:
The text in the Text view does not take up all the space. The word "CARBON" can fit in the first line of the text view but for some reason it goes to the second line. How can I fix this?
In new IOS 14 beta i use ZStack to display badge on my tabs. When keyboard is appear - zstack fly on the keyboard, but it must be fixed in the bottom. IOS 13 - has nothing like this.
ZStack {
Circle()
.foregroundColor(.red)
Text("\(self.realm.ItemsAll.count)")
.foregroundColor(.white)
.font(Font.system(size: 11))
}.frame(width: 15, height: 15)
I am attempting to build a soccer app. My main detail view shows match statistics. I cannot find any way to get the central text element showing the score of the match to stay in the absolute center, so that the colon in the text is perfectly centred over the guideline on the middle of the screen. With different numbers either side of the colon, it gets shifted slightly out of place.
Is there any way to fix this element to be exactly centered under any circumstances? I have attached a screenshot showing the misalignment.
Code:
VStack() {
HStack(alignment: .center) {
Text("\(matchItem.HomeGoals) : \(matchItem.AwayGoals)")
.font(.system(size: 35.0))
.fontWeight(.heavy)
.foregroundColor(Color.white)
.lineLimit(3)
.multilineTextAlignment(.center)
.fixedSize(horizontal: true, vertical: true)
.allowsTightening(false)
}.padding(15)
}.frame(width: 75, height: 80, alignment: .center)
Example:
You can put your View in a ZStack and the ":" on another Z-level, so it no longer depends on other elements in the same horizontal stack.
I am building a fitness app where user selects one of the programs from the list and when he clicks on selected program a new view appears with video playing (video has it's countdown timer on it).
I built the screen using NavigationView/NavigationLink each having it's "destination view" with it's own params.
let sets: [TrainingSet]
init() {
self.sets = [set1,set2,set3]
}
var body: some View {
NavigationView {
VStack {
ForEach(self.sets) { set in
NavigationLink(destination: ExerciseVideoView(items: set.items).navigationBarBackButtonHidden(true)) {
VStack {
Image("group-\(set.image)")
.resizable()
.renderingMode(.original)
.frame(height: 200, alignment: .leading)
.overlay(
Text(set.purpose)
.font(.largeTitle)
.fontWeight(.semibold)
.foregroundColor(.white)
)
Color.blue
.frame(maxWidth: .infinity)
.overlay(
Text(set.purpose)
.foregroundColor(.white)
)
}
}
}
}
}
}
1) I noticed that when the parent view is built, all destination views (ExerciseVideoView) get executed even before user clicks on corresponding button. My countdown timers start in background. I was supposed to see them launched when user click on NavigationLink and new view is "executed". Is that correct behaviour? Can I make destination view "executed/started" when they are shown?
2) The second problem is I wanna show the images with blue buttons below each of them (I put them both inside a VStack container). But when I launch my app only the images are shown but "Color.blue" rectangles are not visible. Why is that? How to make them visible as well?
im trying to show a list of items. Every row should contain a stripe on the left in a specific color and a headline. This headline is sometimes more than a line long but only shows as one line and "...". When I remove the Stripe it shows as multiline text. I've attached the code and two pictures for comparison
Heres my code :
HStack {
Rectangle()
.foregroundColor(poll.outcome ? .green : .red)
.frame(width: 3)
VStack {
Text(poll.poll.title!).font(.headline)
.lineLimit(2)
}
}
This is how it looks without the Rectangle:
And with the Rectangle:
In a List or ScrollView the SwiftUI engine will compress back the text area. .lineLimit(x) give you a maximum of line, not a minimum ;)
To secure that the engine does not shrink back the Text height and goes up to the maximum limit, add .fixedSize(horizontal: false, vertical: true) as shown below
HStack {
Rectangle()
.foregroundColor(.red)
.frame(width: 3)
VStack {
Text("line1\nline2\nline3").font(.headline)
.lineLimit(2)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
}
}
SwiftUI has some bugs right now and it is one of them and there is some discussion on this answer that you can check out.
Although It works on my machine, if you have any trouble about sizing elements, you can use Spacers as a workaround until all SwiftUI bugs fix by Apple.
For this case, you can wrap your text between two spacers like this:
VStack {
Spacer()
Text("FirstLine\nSecondLine\nThirdLine")
.font(.headline)
.lineLimit(2)
Spacer()
}