Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I wanted to ask how to make this navigation button(link) to be in the right up corner?
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: NoteView(), label: {
Text("+")
.bold()
.frame(width: 30, height: 30)
.background(Color.black)
.foregroundColor(.white)
.cornerRadius(10)
})
}
}
}
I tried using ZStack, but it didn't work. I imagine it can be done using spacer, but don't figure out how
To align it in the upper right corner, use a combination of both a VStack and an HStack, with spacers.
Here's how this could be done:
VStack {
HStack {
Spacer() // PUSHES CONTENT TO THE RIGHT
Text("+")
}
Spacer() // PUSHES CONTENT UPWARDS
}
Or...
HStack {
Spacer() // PUSHES CONTENT RIGHT
VStack {
Text("+")
Spacer() // PUSHED CONTENT UP
}
}
Related
I'm trying to find a way to mark which content is overlapped by the keyboard that pops up when a TextField is focused.
Consider the minimal example below:
VStack {
TextField(text: $text) {}
Spacer()
Circle()
.frame(width: 100, height: 100)
}
I'm looking for a custom modifier that I could apply to the Circle. This would dictate whether the keyboard should push the Circle up above the keyboard, as is default, or if the keyboard should overlap the Circle (I'm not sure how to get the keyboard to simply overlap a view, either).
So far I've found primarily Objective-C solutions... Are there any SwiftUI ways to go about this? Thank you for the help!
If I understood correctly your goal then you don't need custom modifier - there is standard modifier, which you can either add or remove, or use with some condition.
VStack {
TextField(text: $text) {}
.textFieldStyle(.roundedBorder)
VStack {
Spacer()
Circle()
.frame(width: 100, height: 100)
}
.ignoresSafeArea(.keyboard, edges: .bottom) // << here !!
//.ignoresSafeArea(yourFlag ? .keyboard : .container, edges: .bottom) // << alternate
}
Build for macOS. I want to make a toolbar like Safari's toolbar, that there are three parts (leading, centered, and trailing). And the centered part stays at the absolutely center, no matter how long leading or trailing part is or changes to.
My code would be like below, but failed to achieve my requirement. As shown in the screenshot below, the trailing placements (.confirmationAction, .primaryAction, .automatic etc.) all don't work. If a ToolbarItem whose placement is .principal exists, the trailing part attach to it closely. Also, I find the .principal is not absolutely centered. The position is depending on the leading part.
How can I solve this and meet the requirement? Or some alternative methods rather than .toolbar{}?
.toolbar {
ToolbarItem(placement: .principal) {
Button {} label: {
Image(systemName: "star.fill")
.scaleEffect(1.5)
}
}
ToolbarItem(placement: .confirmationAction) {
Button {} label: {
Image(systemName: "star")
}
}
}
Using Spacer() is the best option here because it will separate space between your item fairly. Put your images/components between Spacer().You can use this method with other Stack/Container too, not just HStack. NOTE: this is an alternative way relates to poster's request. Try code below the image:
import SwiftUI
struct UsingSpacer__: View {
var body: some View {
VStack {
Spacer()
HStack {
Spacer()
Image(systemName: "square.fill")
Spacer()
Image(systemName: "circle.fill")
Spacer()
Image(systemName: "cloud.fill")
Spacer()
}
.background(.blue)
}
}
}
I am a web developer trying to learn SwiftUI and have some questions regarding about my issue.
So my code architecture is as follows:
ContentView.swift
ScrollView(.vertical){
Foreach(users){
user in UserCard()
}
}
UserCard.swift
VStack(){
Image()
Text("long text")
}
How do I make my UserCard to always the height of my iphone? As of right now, my UserCard sometimes overflow or cuts off too quickly depending on my Text length.
Any help would be appreciated. And any tips for a SwiftUI beginner would be great!
Thank you in advanced.
If you want it to be full screen and ignoring safe area you can set the UserCard's height to screen height like this:
VStack(){
Image()
Text("long text")
}
.frame(height: UIScreen.main.bounds.height)
And if you want it to respect safe area you could use a GeometryReader:
GeometryReader { geometry in
ScrollView {
Color.red
.frame(height: geometry.size.height)
Color.blue
.frame(height: geometry.size.height)
Color.yellow
.frame(height: geometry.size.height)
}
}
one Question -> one Answer
for second part of your Question need new Question with own Topic.
ZStack()
{
VStack()
{
Image(systemName: "star")
Text("long text")
}
}
.ignoresSafeArea()
I am having problems with NavigationLink in Swift and iOS 14. This is a known issue, however the problem solvers that I saw, are not sufficient. I found out that when a button with a NavigationLink is initialized early, it works and when you enter the page, you have the "go back" button. However, when the navigation link is in the middle, you are not able to go back when you enter the page and must close the app to go to the main screen again.
This is the code I am using:
HStack {
Button(action: {
if let url = URL(string: "I blacked this out to make no advert here") {
UIApplication.shared.open(url)
}
}){
Text("Rate App").foregroundColor(.gray)
}.modifier(navigationButtonViewStyle())
Spacer()
// this navigation link works fine
NavigationLink(destination: HelpView()) {
Text("FAQ").foregroundColor(.gray)
}.modifier(navigationButtonViewStyle())
}.padding(.horizontal, 20)
VStack {
Text("λSET")
.font(.largeTitle)
.fontWeight(.heavy)
.bold()
.padding(.top, 50)
.onAppear(perform: prepareHaptics)
.animation(.linear)
Text("λudio System Engineer Tools")
.fontWeight(.light)
.animation(.linear(duration: 0.5))
HStack {
Spacer()
// this navigation link is not showing the go back button when pressed and directed to the new screen
NavigationLink(destination: AboutView()) {
Text("About λSET")
.fontWeight(.heavy)
.bold()
}
Spacer()
}.modifier(calcTitleViewStyle())
.padding(.top, 20)
.padding(.bottom, 60)
}
I am not sure what the problem is, but am curious what this could be. I am working on a workaround, but I would love to solve the problem at its roots.
Thanks!
Fixed it myself by explicitly setting the .NavigationBar to true to force swift to load it. This was not needed in other pages, but I am now doing to make sure IOS14 accepts it, since a few things have been changed there.
So I was trying to implement expanding/contracting rows in a for each embedded in a list view and it turns out the list view cells don't animate smoothly. After checking out this tutorial, it suggests using a scrollview with a foreach because it animates smoother. The expanding/contracting works fine, but there unintended side effects only when I first open the page. The HStack appears to start flattened on the left side of the view and animates expanding to its normal starting position. I've slowed down the animations in the video so it's easier to see, and I removed the ForEach since it doesn't seem to be the cause of the problems. I don't know what is causing this and google searching has yielded no answers. Does anyone here have an answer or at least some advice? Much appreciated
#State var showTemp: Bool = false
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
HStack(alignment: .top) {
Text("Hello")
.font(.system(size: 32))
if showTemp {
VStack {
Text("Middle 1")
Text("Middle 2")
Text("Middle 3")
Text("Middle 4")
Text("Middle 5")
}
}
Spacer()
Text("Goodbye")
}
.border(Color.black, width: 2)
.onTapGesture {
self.showTemp.toggle()
}
.background(Color.blue)
.animation(.default)
}
.background(Color.green)
.onAppear(perform: loadMethod)
}
It seems I've answered my own question after I looked at the problem from a different angle. Using GeogetryReader and setting the frame width of the HStack to geometry.size.width fixed it.
I think this works because without specifying the width, the HStack fills as much area as it can. For some reason, it starts smushed when the view is loaded and then expands to fill its area, which I guess isn't visible unless you apply an animation to it.
Trying to be helpful here, I don't want to be one of those people who answers their own questions but doesn't show what the answer it.