首页 > 其他分享 >SwiftUI ScrollView 滚动视图

SwiftUI ScrollView 滚动视图

时间:2024-05-07 21:55:43浏览次数:26  
标签:category author image ScrollView SwiftUI 视图 var heading

代码

//
//  ContentView.swift
//  SwiftUIScrollView
//
//  Created by CHEN Hao on 2024/5/7.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(alignment: .leading) {
            VStack(alignment: .leading) {
                Text("monday, aug 20".uppercased())
                    .font(.caption)
                    .foregroundStyle(.secondary)
                Text("Your Reading")
                    .font(.system(.largeTitle,design: .rounded))
                    .fontWeight(.black)
                    .foregroundStyle(.primary)
            }
            .padding()
            
            // 滚动试图 .horizontal--横向滚动,.vertical--垂直滚动,默认vertical
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    Group { // 群组视图 群组内视图设置统一样式
                        CardView(image: "swiftui-button", category: "SwiftUI", heading: "Drawing a Border with Rounded Corners", author: "Simon Ng")
                        CardView(image: "macos-programming", category: "macOS", heading: "Building a Simple Editing App", author: "Gabriel Theodoropoulos")
                        CardView(image: "flutter-app", category: "Flutter", heading: "Building a Complex Layout with Flutter", author: "Lawrence Tan")
                        CardView(image: "natural-language-api", category: "iOS", heading: "What's New in Natural Language API", author: "Sai Kambampati")
                    }
                    .frame(width: 300)
                }
            }
            Spacer()
        }
    }
}

#Preview {
    ContentView()
}

struct CardView: View {
    var image: String
    var category: String
    var heading: String
    var author: String
    
    var body: some View {
        VStack{
            Image(image)
                .resizable()
                .aspectRatio(contentMode: .fit)
            HStack {
                VStack(alignment: .leading) {
                    Text(category)
                        .font(.headline)
                        .foregroundStyle(.secondary)
                    Text(heading)
                        .font(.title)
                        .fontWeight(.black)
                        .foregroundStyle(.primary)
                        .lineLimit(3)
                        .minimumScaleFactor(0.5) // 自动缩小文字
                    Text(author.uppercased())
                        .font(.caption)
                        .foregroundStyle(.secondary)
                    
                }
                Spacer()
            }
            .padding()
        }
        .clipShape(RoundedRectangle(cornerRadius: 10)) // 剪裁成圆角矩形
        .overlay(RoundedRectangle(cornerRadius: 10)
            .stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.1), lineWidth: 1)
        ) // 描边(就是覆盖一个视图)
        .padding([.top,.horizontal])
    }
}

效果

标签:category,author,image,ScrollView,SwiftUI,视图,var,heading
From: https://www.cnblogs.com/thankcat/p/18178487

相关文章

  • SwiftUI ZStack、HStack、VStack 布局
    代码////ContentView.swift//SwiftUIStacks////CreatedbyCHENHaoon2024/5/6.//importSwiftUIstructContentView:View{varbody:someView{VStack(spacing:15){HeaderView()HStack(spacing:15){......
  • 如何为数据库中新建用户B复制用户A的表和视图权限?
    故事背景:公司使用的是SQLServer数据库,经常会碰到一种情况,需要为新入职的员工赋予同组内其他同事的权限。  常用方法:1,为同一组申请创建统一的SecurityGroup(安全组),为创建的组分配相关表和视图的访问权限。不管员工入职还是离职,仅需将组内的成员进行相关的添加和删除即可......
  • SwiftUI Image 图片处理
    代码片段////ContentView.swift//SwiftUIImage////CreatedbyCHENHaoon2024/5/6.//importSwiftUIstructContentView:View{varbody:someView{Image("paris").resizable()//延伸模式,平铺整个屏幕/......
  • SwiftUI Text 文字处理
    代码////ContentView.swift//SwiftUIText////CreatedbyCHENHaoon2024/5/6.//importSwiftUIstructContentView:View{varbody:someView{VStack{Text("Yourtimeislimited,sodon’twasteitlivingsomeoneels......
  • Oracle 给用户授权访问视图
    Oracle给用户授权访问视图createuserc##hrpidentifiedby123456;grantresource,connecttoc##hrp;grantconnecttoc##hrp;grantselectonC##BSKJ.sch_sc_schedulinfotoc##hrp;grantselectonC##BSKJ.sch_sc_classestoc##hrp;grantselectonC##BSKJ.sys_usert......
  • ASP.NET Core Razor Pages 使用 视图(View) 组件
    参考文章地址:为什么要在ASP.NETCore中使用视图组件(telerik.com)为什么使用视图组件而不是分部视图?最大的原因是,在Razor页面中插入分部视图时,与调用View关联的所有ViewData都会自动与分部视图关联。这意味着分部视图在一个Razor页面上的行为可能与在另一个页面上的行......
  • 深入学习和理解Django视图层:处理请求与响应
    title:深入学习和理解Django视图层:处理请求与响应date:2024/5/417:47:55updated:2024/5/417:47:55categories:后端开发tags:Django请求处理响应生成模板渲染表单处理中间件异常处理第一章:Django框架概述1.1什么是Django?Django是一个高级的PythonWeb......
  • 视图查询优化-不带条件
    视图查询优化-不带条件sqlselect/*+VIEW_FILTER_MERGING(1)NO_USE_CVT_VAR*/v.account_number,v.bill_id,v.account_id,v.bill_line_id,v.bill_number,v.business_type,v.business_type_sub,v.EVENT_ID,v......
  • 视图优化带条件
    sqlselectv.account_number,v.bill_id,v.account_id,v.bill_line_id,v.bill_number,v.business_type,v.business_type_sub,v.EVENT_ID,v.PROJECT_ID,v.OBJECT_TYPE,v.OBJECT_ID,v......
  • Django - 探究CBV视图
    目录数据显示视图基础视图TemplateView数据显示视图基础视图TemplateView视图类TemplateView是所有视图类里最基础的应用视图类,开发者可以直接调用应用视图类,它继承多个父类classTemplateView(TemplateResponseMixin,ContextMixin,View):"""Renderatemplate......