小编Pan*_*ruz的帖子

SwiftUI - 为什么键盘会推动我的视图?

为什么我的键盘会推高我的视图?这是代码:

import SwiftUI

struct ContentView : View {
    @State var searchText = ""
    @State var pressedFirstButton = false
    @State var pressedSecondButton = true
    @State var pressedThirdButton = false
    
    
    var body : some View {
        NavigationView {
            VStack {
                if pressedSecondButton == true {
                Form {
                    SearchBar(testo: $searchText)
                        .padding(.horizontal, -10)
                    
                    Text("ForEach")
                }
                }
                HStack {
                    Spacer()
                    buttonFirst
                    Spacer()
                    buttonSecond
                    Spacer()
                    buttonThird
                    Spacer()
                }
                
            }.navigationBarTitle("Search")
        }
    }
    
    var buttonFirst : some View {
        Button(action: {
            self.pressedFirstButton = true
            self.pressedSecondButton = false
            self.pressedThirdButton …
Run Code Online (Sandbox Code Playgroud)

keyboard searchbar swift swiftui

7
推荐指数
1
解决办法
5067
查看次数

SwiftUI - 如何在表单中添加“字母部分”和字母跳线?

如何制作一个表单,其中元素根据其首字母自动分为几个部分,并在右侧添加字母跳线以显示以所选字母开头的元素(就像联系人应用程序一样)?

\n

我还注意到一件奇怪的事情,我不知道如何重新创建:并非所有字母都显示出来,其中一些显示为“\xe2\x80\xa2”。但是,当您点击它们时,它们无论如何都会带您到相应的字母。我尝试在 ZStack 中使用 ScrollView(.vertical) 并将 .scrollTo(selection) 添加到按钮的操作中,但是 1) 它没有滚动到我想要的选择 2) 当我点击“\xe2 \x80\xa2",就好像我正在点击所有它们,因为它们都做了点击动画 3) 我无法按照我想要的方式划分列表。\n我有这个:

\n
import SwiftUI\n\nstruct ContentView: View {\n\nlet alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W", "X","Y", "Z"]\nlet values = ["Avalue", "Bvalue", "Cvalue", "Dvalue"]\n\nvar body: some View {\n           ScrollViewReader{ scrollviewr in\n               ZStack {\n                   ScrollView(.vertical) {\n                       VStack {\n                           ForEach(alphabet, id: \\.self) { letters in\n                               Button(letters){\n                                   withAnimation {\n                                    scrollviewr.scrollTo(letters)\n                                   }\n                               }\n                           }\n                       }\n                   }.offset(x: 180, y: 120)\n\n                VStack {\n                    \n                ForEach(values, id: \\.self){ vals in\n                               Text(vals).id(vals)\n                   }\n                }\n               }\n           }\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但我想要这样的: …

menu scrollview alphabet swiftui

5
推荐指数
1
解决办法
4147
查看次数

SwiftUI - 按下按钮时移动和旋转图像

我有一个图像,我希望它在 y 轴上旋转,并在按下按钮时移向视图的底部。我尝试使用 .onChange,但出现错误“调用 'animation' 的结果未使用”,我理解其中的含义,但我既不明白它为什么会出现,也不明白如何解决它。

这是我的代码:

import SwiftUI

struct ContentView : View {

    @State var animateCoin = false
    @Environment(\.colorScheme) var colorScheme

        var body: some View {

 Rectangle()
        .foregroundColor(colorScheme == .dark ? .white : .black)
        .frame(width: 150, height: 150, alignment: .center)
        .offset(y: -60)
        .mask(Image("Coin") //That's the name of the Image I have in the assets
                      .resizable()
                      .onChange(of: animateCoin, perform: { value in
                            self.animation(.easeIn) //I put .easeIn just as an example, because the .rotation3DEffect gives me a red warning
                        }))

Button(action: …
Run Code Online (Sandbox Code Playgroud)

animation image rotation button swiftui

2
推荐指数
1
解决办法
520
查看次数