第十三章 Button|cornerRadius

登陆页面剩下就记住密码和登录按钮了,我们接下来封装记住密码组件。我们在 LoginPage 的 View 新建一个 RememberPasswordContentView.swift.

swift 复制代码
struct RememberPasswordContentView: View {
    @Binding var isRememberPassword:Bool
    @StateObject private var appColor:AppColor = AppColor.share
    var body: some View {
        HStack {
            Rectangle()
                .frame(width:15 ,height:15)
                .foregroundColor(foregroundColor)
                .border(Color(uiColor: appColor.c_cccccc), width: 1)
            Text("记住密码")
                .foregroundColor(Color(uiColor: appColor.c_cccccc))
        }
      	.onTapGesture {
            isRememberPassword.toggle()
        }
    }
    
    private var foregroundColor: Color {
        isRememberPassword ? Color(uiColor: appColor.c_209090) : .clear
    }
}

使用 Rectangle 设置选中和未选中状态

我们让 Rectangle 有边框无前景色作为未选中状态,将 Rectangle 有边框有前景色作为选中状态。

我们将记住密码的组件和 用户名和密码放在一起,左侧对齐。

我们发现记住密码和输入密码组件的间隙也是 30。这个对于我们来说,间隙太大了,我们同时为了让 记住密码的组件点击范围增加,我们设置 Padding如下。

swift 复制代码
RememberPasswordContentView(isRememberPassword: $viewModel.isRememberPassword)
    .padding(EdgeInsets(top: -20, leading: 0, bottom: 10, trailing: 0))

使用 Button 封装登录按钮

下面我们添加登录按钮的组件。

swift 复制代码
Button("登录") {
}
.frame(maxWidth:.infinity)
.frame(height: 45)
.background(Color(uiColor: appColor.c_209090))
.foregroundColor(.white)
.cornerRadius(5)

这里我们用到了Button 的组件,对于 Button的创建有两种。

文本类型

swift 复制代码
Button("按钮") {}

自定义内容

swift 复制代码
Button {
} label: {
    HStack {
        Image("password_icon_34x34")
        Text("密码按钮")
    }
}

修复按钮其他区域无法点击

本来我想着这个按钮就是简单的一个文本按钮,结果发现运行只有点击文本的地方才有反应,因此按钮的响应区域在于Label组件的大小。

使用 cornerRadius 设置圆角

我们修改创建的按钮的代码如下。

swift 复制代码
 Button {
  } label: {
      Text("登陆")
          .frame(maxWidth:.infinity)
          .frame(height: 45)
          .background(Color(uiColor: appColor.c_209090))
          .foregroundColor(.white)
          .cornerRadius(5)
  }

虽然是同样的按钮,但是上面的代码整个按钮点击都有响应。

到此我们对于登陆页面的界面已经做完了,虽然简单,但是我们竟然用到那么多的控件和知识。接下来,我们就将登陆界面的逻辑完成。

相关推荐
东坡肘子18 小时前
当 Build in Public 重新转入地下 -- 肘子的 Swift 周报 #157
人工智能·swiftui·swift
OliverQiu1 天前
开源一个 macOS 小工具:集中管理 ChatGPT 的网络启动配置
macos·chatgpt·swiftui·mac·proxy·codex·dmg
传奇开心果编程5 天前
【SwiftUI提高练中学】第9课 安全与隐私
swiftui·移动开发·swift·编程语言理论·开发范式·编程框架·声明式ui
传奇开心果编程6 天前
【声明式UI开发实用技术学与练】第8课 状态提升与下放
学习·flutter·react native·ui·swiftui·composer
东坡肘子6 天前
Swift Server,又多了一个 Google -- 肘子的 Swift 周报 #156
人工智能·swiftui·swift
传奇开心果编程7 天前
【现代声明式UI学与练】第4课 列表渲染与 key——如何高效渲染列表、key 的作用、列表重排时的状态保持
学习·flutter·react native·ui·swiftui·android jetpack
传奇开心果编程7 天前
【SwiftUI提高练中学】第8课 网络层架构:拦截器、重试、缓存与离线优先
学习·macos·ui·ios·swiftui
传奇开心果编程7 天前
【现代声明式UI学与练】第7课 组件通信——父传子、子传父、跨层级通信、状态提升与 Context
学习·flutter·react native·ui·swiftui·android jetpack
tink8 天前
告别 Xcode IDE:用 VS Code + SweetPad + XcodeGen 开发 iOS 应用的完整指南
ios·swiftui·xcode
传奇开心果编程8 天前
【现代声明式UI学与练】第9课 性能优化——渲染优化、列表优化、内存优化、启动优化
学习·flutter·react native·ui·性能优化·swiftui·android jetpack