小编mar*_*ars的帖子

如何在 SwiftUI 中将 Apple ColorPicker 的布局从圆形更改为方形?

ColorPicker默认的按钮样式是圆形,如下所示。

\n

在此输入图像描述

\n

我想将圆形按钮的样式更改为矩形。但似乎没有 API 可以改变它的风格。所以我在它上面放了一个矩形,并将它的 allowedHitTesting 设置为 false 以将单击事件传输到 ColorPicker。

\n

在此输入图像描述

\n
struct ColorPickerView: View {\n    @State private var colorValue = Color.orange\n    var body: some View {\n        ZStack() {\n            ColorPicker("", selection: $colorValue)\n                .labelsHidden()\n            Rectangle()\n                .foregroundColor(.blue)\n                .frame(width: 40, height: 40, alignment: .center)\n                .allowsHitTesting(false)\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但是点击后ColorPicker没有出现。

\n

我在矩形下面画了一个圆圈来测试 allowedHitTesting 是否有用。它可以正常响应点击手势打印“Circle tapped!”。

\n
struct ColorPickerView: View {\n    @State private var colorValue = Color.orange\n    var body: some View {\n        ZStack() {\n            ColorPicker("", selection: $colorValue)\n                .labelsHidden()\n            Rectangle()\n                .foregroundColor(.blue)\n                .frame(width: 40, height: 40, …
Run Code Online (Sandbox Code Playgroud)

color-picker swiftui

7
推荐指数
1
解决办法
3584
查看次数

带有自定义标题的 swiftui 列表

我想在 swiftui 中为列表设置图像标题。我想要的效果如下图所示:

在此输入图像描述

但是,我无法删除该图像行中的填充。我的代码如下:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Section() {
                    Image("HeadImage")
                        .resizable()
                        .frame(height: 150)
                }
                ForEach((0..<4), id: \.self) { index in
                    Section {
                        NavigationLink(destination: Text("aaa")) {
                            Label("Buttons", systemImage: "capsule")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Colors", systemImage: "paintpalette")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Controls", systemImage: "slider.horizontal.3")
                        }
                    }
                }
            }
            .navigationBarTitle("SwiftUI")
            .navigationBarTitleDisplayMode(.inline)
        }
        .accentColor(.accentColor)
    }
}
Run Code Online (Sandbox Code Playgroud)

但图像行有一个填充,如下所示:

在此输入图像描述

有什么方法可以去除这个填充吗?

swiftui swiftui-list swiftui-navigationview

7
推荐指数
1
解决办法
1万
查看次数

混合 UIKit 和 SwiftUI 项目中导航栏按钮出现延迟

我有一个应用程序,其主要框架是使用uikit开发的,其一些页面是由SwiftUI编写的,并嵌入到uikit中通过UIHostingController显示。现在有一个Swiftui页面,我想通过UIHostingController推送到这个页面。

窗口初始化代码如下:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        
        let vc = UINavigationController(rootViewController: ViewController())
        
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
        return true
    }
Run Code Online (Sandbox Code Playgroud)

然后是 SwiftUI 视图:

struct ContentView: View {
    var body: some View {
        Text("hello world")
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是UIKit vc:

import UIKit
import SwiftUI

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(runBtn)
        runBtn.frame = CGRect(x: 100, y: 200, width: 200, height: 50)
    }
    
    lazy var runBtn: UIButton  = …
Run Code Online (Sandbox Code Playgroud)

uikit swiftui uihostingcontroller

5
推荐指数
1
解决办法
419
查看次数