小编Rij*_*uel的帖子

如何在最新的 iOS 14 中的 swiftui 中仅圆化滚动视图的顶角

我试图只圆化滚动视图的顶角。在 .clipShape() 修饰符中提供自定义形状过去在早期的 iO​​S 版本中工作得很好,但在最新的 iOS 14 中,滚动视图的底部内容正在消失。请建议任何解决方法

以下是用于圆化特定角的自定义形状:

struct RoundedCorner: Shape {
    
    var radius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners
    
    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}

extension View {
    func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
        clipShape( RoundedCorner(radius: radius, corners: corners) )
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是实现自定义圆角修饰符的示例视图的代码:

struct SampleView: View {
    
    @State var fullname: String
    @State var email: String …
Run Code Online (Sandbox Code Playgroud)

swift swiftui

9
推荐指数
1
解决办法
5433
查看次数

如何使用 SwiftUI 应用程序生命周期 XCode 12 将 SceneDelegate 添加到 SwiftUI 应用程序

我使用 SwiftUI 应用程序生命周期在 SwiftUI 中创建了一个应用程序。有没有办法将 SceneDelegate 类添加到主构造中。添加 AppDelegate 似乎是可能的

import SwiftUI

@main
struct SampleApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
                HomeView()
                // .colorScheme(.light)
        }
    }
}

// App Delegate class for conventional lifecycle capturing
final class AppDelegate: NSObject, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        NetworkManager.shared.startMonitoring()
        return true
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        NetworkManager.shared.stopMonitoring()
    }
}
Run Code Online (Sandbox Code Playgroud)

swift swiftui

7
推荐指数
0
解决办法
4097
查看次数

如何使自定义导航栏背景颜色超出 SwiftUI 中的安全区域

我实现了一个自定义导航栏,它只是一个带有标题文本和用于关闭视图的箭头的视图。我只希望导航栏的背景颜色超出顶部安全区域,同时内容遵守安全区域限制

这是自定义导航栏的代码:

struct CustomNavigationBar: View {
    
    @Environment(\.presentationMode) var presentationMode
    let title: String
    
    var body: some View {
        HStack {
            Button(action: {
                presentationMode.wrappedValue.dismiss()
            }, label: {
                Image(systemName: "chevron.left")
                    .font(.title2)
                    .foregroundColor(Globals.Color.background)
            })
            Spacer()
            Text(title)
                .font(.title2)
                .bold()
                .foregroundColor(Globals.Color.background)
            Spacer()
        }
        .padding()
        .background(Globals.Color.crimson)
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是使用自定义导航栏的示例视图的代码:

var body: some View {
    ZStack {
        VStack {
            CustomNavigationBar(title: "Sample Title")
            Spacer()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

下面我附上了当前视图的屏幕截图:

导航栏视图的红色背景停在安全区域]

swift safearealayoutguide swiftui

6
推荐指数
1
解决办法
1915
查看次数

标签 统计

swift ×3

swiftui ×3

safearealayoutguide ×1