WatchOS 26中buttonStyle修饰器仅在物理设备上延迟执行的问题
WatchOS 26 带
.borderedProminent样式按钮首次点击延迟问题 问题详情
在 Apple Watch Ultra 3(WatchOS 26)真机上运行以下代码时遇到异常:
- 应用刚安装完成后,第一次点击"Toggle"按钮,从按钮按下到"SHOWN"文本显示会有几秒延迟,之后再点击就完全正常
- 把按钮的
.buttonStyle(.borderedProminent)去掉后,延迟问题直接消失 - 模拟器上不管有没有加这个样式,都不会出现延迟
代码示例:
struct ContentView: View { @State private var showText = false var body: some View { VStack(spacing: 20) { Button("Toggle") { print("button pressed") showText.toggle() } .buttonStyle(.borderedProminent) Text("✅ SHOWN") .font(.title) .foregroundStyle(.green) .opacity(showText ? 1 : 0) } }
可行的解决办法
- 提前预加载样式资源:首次延迟基本是因为系统第一次使用
.borderedProminent样式时,才去加载对应的渲染素材、动画逻辑等资源。可以在应用启动时提前触发这个加载流程,比如在ContentView的onAppear里加个隐藏的同样式按钮:
.onAppear { // 预加载borderedProminent样式的资源 let _ = Button("") {}.buttonStyle(.borderedProminent) }
- 用自定义样式替代:如果预加载不管用,不如自己写个类似
.borderedProminent的按钮样式,绕开系统样式的首次加载开销:
struct CustomProminentButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label .padding(.horizontal, 16) .padding(.vertical, 8) .background(configuration.isPressed ? Color.blue.opacity(0.7) : Color.blue) .foregroundColor(.white) .cornerRadius(12) } } // 使用时替换原样式: .buttonStyle(CustomProminentButtonStyle())
- 等待系统更新:WatchOS 26属于测试版本,很可能是系统UI组件的加载bug,后续正式版或者beta更新大概率会修复这个问题,确保你的Xcode用的是对应版本的beta工具链就行。
内容的提问来源于stack exchange,提问作者JManke
相关产品推荐
相关产品推荐

