You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 18模态界面滚动起始于按钮时触发按钮点击问题咨询

iOS 18模态界面滚动起始于按钮时意外触发点击的异常问题

在iOS 18系统中,当界面以模态(modal)形式展示时,若滚动操作的起始点落在按钮区域内,该按钮会被意外触发点击事件;非模态界面无此问题,且旧版iOS无论模态或非模态场景下均不会出现该异常。

复现代码

import SwiftUI

struct ContentView: View {
    
    @State var presentModal = false
    
    var body: some View {
        Button(action: {
            presentModal = true
        }, label: {
            Text("open modal")
        })
        .sheet(isPresented: $presentModal, content: {
            ScrollView {
                ForEach(0..<100, id: \.self) { index in
                    Button(action: {
                        print("Button \(index) tapped!")
                    }) {
                        Text("Button \(index)")
                            .frame(maxWidth: .infinity)
                            .frame(height: 100)
                            .background(randomColor(for: index))
                            .padding(.horizontal)
                    }
                }
            }
        })
    }
    
    func randomColor(for index: Int) -> Color {
        let hue = Double(index % 100) / 100.0
        return Color(hue: hue, saturation: 0.8, brightness: 0.8)
    }
}

#Preview {
    ContentView()
}

临时修复方案

针对这个iOS 18的系统级问题,可以通过以下两种方式临时规避:

  • 给按钮添加高优先级拖动手势:让滚动手势优先被识别,阻止起始于按钮的滚动操作触发点击事件:

    Button(action: {
        print("Button \(index) tapped!")
    }) {
        Text("Button \(index)")
            .frame(maxWidth: .infinity)
            .frame(height: 100)
            .background(randomColor(for: index))
            .padding(.horizontal)
    }
    .highPriorityGesture(DragGesture())
    
  • 改用TapGesture实现点击逻辑:放弃Button的默认交互,通过.onTapGesture绑定点击逻辑,避免滚动时误触:

    Text("Button \(index)")
        .frame(maxWidth: .infinity)
        .frame(height: 100)
        .background(randomColor(for: index))
        .padding(.horizontal)
        .onTapGesture {
            print("Button \(index) tapped!")
        }
    

内容的提问来源于stack exchange,提问作者Claudiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 16:48:27