ColorPicker默认的按钮样式是圆形,如下所示。
\n\n我想将圆形按钮的样式更改为矩形。但似乎没有 API 可以改变它的风格。所以我在它上面放了一个矩形,并将它的 allowedHitTesting 设置为 false 以将单击事件传输到 ColorPicker。
\n\nstruct 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}\nRun Code Online (Sandbox Code Playgroud)\n但是点击后ColorPicker没有出现。
\n我在矩形下面画了一个圆圈来测试 allowedHitTesting 是否有用。它可以正常响应点击手势打印“Circle tapped!”。
\nstruct 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) 我想在 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)
但图像行有一个填充,如下所示:
有什么方法可以去除这个填充吗?
我有一个应用程序,其主要框架是使用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)