第十三章 Button|cornerRadius

登陆页面剩下就记住密码和登录按钮了,我们接下来封装记住密码组件。我们在 LoginPageView 新建一个 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)
  }

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

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

相关推荐
用户79457223954132 天前
【SnapKit】优雅的 Swift Auto Layout DSL 库
swiftui·swift
Mr_Tony2 天前
iOS / SwiftUI 输入法(键盘)布局处理总结(AI版)
ios·swiftui
东坡肘子2 天前
苹果的罕见妥协:当高危漏洞遇上“拒升”潮 -- 肘子的 Swift 周报 #130
人工智能·swiftui·swift
曾经我也有梦想9 天前
SwiftUI 如何使用 UIKit 组件
swiftui
东坡肘子9 天前
一墙之隔,不同的时空 -- 肘子的 Swift 周报 #129
人工智能·swiftui·swift
曾经我也有梦想10 天前
ViewModifier 和 圆角以及渐变色
swiftui
zhangjikuan8912 天前
SwiftUI 状态管理与架构实战
ios·架构·swiftui
大熊猫侯佩12 天前
浣熊市生存手册:在 Xcode 预览中驯服“支离破碎”的 AI 流式数据
swiftui·stream·xcode 26·generable·foundationmodel·xcode previews·partially gener
曾经我也有梦想14 天前
ObservableObject @Published @ObservedObject那些事
swiftui
曾经我也有梦想14 天前
@Binding 的那些事
swiftui