AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77955779
Accepted
Swift
Swift
Asked: 2024-02-07 22:55:48 +0800 CST2024-02-07 22:55:48 +0800 CST 2024-02-07 22:55:48 +0800 CST

SwiftUI 的 UI 设计面临的问题

  • 772

我在使用 SwiftUI 进行 UI 设计时一直遇到问题。我觉得 UIKit 比 SwiftUI 更好一些,因为使用 SwiftUI 我无法在每个堆栈单独计算空间时给出适当的空间。我不明白如何为其相邻元素提供空间。

我的主要问题是从其相邻元素获取空间,我们是否需要提供前导空间来减去其相邻元素宽度区域?

例如,我已将图像堆栈前导、顶部指定为 20,宽度和高度指定为 100。现在,如果我想添加指向该图像的标签,那么我是否需要将标签前导空格指定为 -100?空格如何提供正确的前导、尾随、顶部、底部?

我在这里使用 UIKit 做了这样的简单设计,如果您只将顶部蓝色视图人物图像置于顶部并导致 20 个,其余标签和图标应该导致该图像视图达到 15 个

在此输入图像描述

现在我只尝试了 SwiftUI 中相同的顶部蓝色设计:我是 UI 新手,所以我尝试了这种方式。如果有任何捷径请告诉我。

如果有任何不必要的代码,请告诉我,并指导我如何使用 SwiftUI 编写简短的代码。

您能否指导我如何将堆栈相互嵌入。有没有什么模式可以将一个模式添加到另一个模式中?

通过这段代码,我也没有得到正确的设计。请指导我修复它。

struct SecondView: View {
    var body: some View {
        ZStack{
            Color.yellow.ignoresSafeArea()
            
            ScrollView{
                VStack{
                    
                    ZStack {
                        Color.blue // Background color of the view
                        
                        VStack{
                            HStack (){
                                Image(systemName: "person.circle.fill")
                                
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 70, height: 70)
                                    .padding(.leading, 10)
                                    .padding(.top, 20)
                                
                                Spacer()
                            }
                            
                            Spacer()
                        }
                        
                        VStack(spacing: 10){
                            
                            Text("Jhon Thomas").foregroundColor(Color.white)
                                .padding(.bottom, 10).font(.system(size: 20))
                            
                            HStack (){
                                Image(systemName: "phone.fill")
                                
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 17, height: 17)
                                    .padding(.leading, 10)
                                Text("0000000000").foregroundColor(Color.white)
                            }
                            HStack (){
                                Image(systemName: "bag.fill")
                                
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 17, height: 17)
                                    .padding(.leading, 10)
                                Text("Singer").foregroundColor(Color.white)
                            }
                            HStack (){
                                Image(systemName: "location.fill")
                                
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 17, height: 17)
                                    .padding(.leading, 10)
                                Text("test loc").foregroundColor(Color.white)
                                Spacer()
                                Image(systemName: "map.fill")
                                
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 17, height: 17)
                                    .padding(.leading, 10)
                                Text("Jhon Thomas").foregroundColor(Color.white)
                            }
                        }.padding()
                        //  .padding(.leading, -80)
                     }.padding()
                     .frame(height: 200)
                }
            }
        }
    }
}

SwiftUI 仅蓝色设计:如何像上面的设计一样使所有名称和图标保持相同的对齐方式,并且仅将 15 个引导到 imageview?

在此输入图像描述

user-interface
  • 1 1 个回答
  • 42 Views

1 个回答

  • Voted
  1. Best Answer
    MatBuompy
    2024-02-08T00:33:38+08:002024-02-08T00:33:38+08:00

    我是来帮忙的!首先,我建议您学习 SwiftUI 的基础知识,请访问此处:SwiftUI 简介,以及此处:SwiftUI 教程。现在,处理手头的问题。我开始使用 SwiftUI 进行 iOS 开发,相信我,现在我已经学习并每天使用 UIKit 进行工作,没有比这更糟糕的事情了!关于您的代码,还有很多可以改进的地方。我借此机会编写了一些自定义修饰符,以避免一遍又一遍地对图像重复相同的修饰符。我删除了冗余堆栈,为那些重复的 HStack 创建了一个可重复使用的视图。我现在要向您展示:

    struct SecondView: View {
        var body: some View {
            ZStack {
                
                // This is ok but you can use .background() modifier too
                //Color.yellow.ignoresSafeArea()
                
                ScrollView {
                    
                    HStack {
                        //Color.white // Background color of the view
                        
                        VStack {
                            /// Alignment top to keep the imafge
                            HStack(alignment: .top) {
                                Image(systemName: "person.circle.fill")
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .modifier(ImageModifier(width: 70, height: 79))
                                    .padding(.top, 20)
                                
                                /// Removed this to avoid pushing the texts and icon to the right
                                //Spacer()
                                
                                /// I used this line to show you the separations of two
                                Rectangle()
                                    .background(.red)
                                    .frame(width: 1)
                            }
                            
                            /// I used the frame modifier instead
                            //Spacer()
                        }
                        /// I used this instead of the Spacer above to achieve the same result
                        .frame(maxHeight: .infinity, alignment: .top)
                        
                        VStack(spacing: 10) {
                            
                            Group {
                                
                                Text("Jhon Thomas")
                                //.foregroundColor(Color.white)
                                    /// Another way of setting padding. I had to use padding leading 10 to match the RowItems
                                    .padding(EdgeInsets(top: 0, leading: 10, bottom: 10, trailing: 0))
                                    .font(.system(size: 20))
                                
                                RowItem(text: "000000000", icon: "phone.fill")
                                
                                RowItem(text: "Singer", icon: "bag.fill")
                            }
                            .frame(maxWidth: .infinity, alignment: .leading)
                            
                            HStack {
                                RowItem(text: "Jhon Thomas", icon: "location.fill")
                                
                                Spacer()
                                
                                RowItem(text: "Jhon Thomas", icon: "map.fill")
                                
                                
                            }
                        }
                        .frame(maxWidth: .infinity, alignment: .leading)
                    }
                    /// Padding placed here (before the frame) will push views to the inside. Modifier order matters in SwiftUI.
                    .padding()
                    .frame(height: 200)
                    /// Padding placed here will make this view larger. The default padding value is 16.
                    //.padding()
                    .background(.white)
                    .frame(maxWidth: .infinity, alignment: .leading)
                }
            }
            .background(.yellow)
        }
        
        @ViewBuilder
        private func RowItem(text: String, icon: String) -> some View {
            HStack {
                Image(systemName: icon)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .modifier(ImageModifier())
                
                Text(text)
                    .foregroundStyle(.black)
            }
        }
        
    }
    

    这是图像的自定义修饰符:

    struct ImageModifier: ViewModifier {
        
        var width: CGFloat = 17
        var height: CGFloat = 17
        
        func body(content: Content) -> some View {
            content
                .frame(width: width, height: height)
                .padding(.leading, 10)
        }
    }
    

    当然,这是为了让您了解如何在 SwiftUI 中完成任务。如果您现在知道如何使用它,您就可以非常非常快地构建 UI 。我的观点是,它为您提供了几乎无限的可能性来控制屏幕上的每个像素。

    详细介绍我所做的事情,我用框架修饰符替换了 Spacers,指定 maxWidth 或 maxHeight 以占用所有可用空间。框架修改器还允许您指定对齐方式。另外,除非您的目标是 iOS 14,否则您可以使用 foregroundStyle 来修改视图的颜色。对于背景,你可以做基本相同的事情,还有一个背景修改器。背景修改器用于在其应用的视图下方绘制内容,而覆盖修改器则在顶部绘制。您可以使用@ViewBuilder宏创建返回视图的函数。我在这里有一个谈论它的答案:ViewBuilder。

    有太多的话要说,但你能做的最好的事情就是亲自动手并大量编码!希望我的回答对朋友有用!

    • 1

相关问题

  • 在 Rust 中,如何获取 TextEdit 小部件 Slint gui 的文本

  • egui中如何使用鼠标拖动窗口?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve