iOS26适配指南之UIScrollView

介绍

在 iOS 26 中,UIScrollView 迎来了两个重要增强,主要聚焦于滚动边缘的视觉效果和与浮动容器的交互体验。这些改进不仅让滚动过渡更自然,还提升了在复杂界面中内容的可读性。

UIScrollEdgeEffect

过去,当内容滚动到边缘时,系统会使用回弹(bounce)或发光(glow)效果来提示用户。但在 iOS 26 中,Apple 增加了类型为UIScrollEdgeEffect的属性topEdgeEffectleftEdgeEffectbottomEdgeEffectrightEdgeEffect,用于设置滚动边缘的效果。

UIScrollEdgeElementContainerInteraction

除了边缘效果,Apple 还引入新的交互UIScrollEdgeElementContainerInteraction,可以将导航栏、标签栏、工具栏或者自定义浮动视图标记为边缘元素容器,当内容滚动到该容器下方时,系统会自动添加一个渐变模糊效果,从而保证叠加内容的可读性。

案例

代码

swift 复制代码
import UIKit

class ViewController: UIViewController {
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(contentView)
        scrollView.contentSize = contentView.frame.size
        // iOS26新增
        // scrollView.bottomEdgeEffect.style = .hard
        return scrollView
    }()

    lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    lazy var stackView: UIStackView = {
        var config = UIButton.Configuration.clearGlass()
        config.title = "Action"
        let button1 = UIButton(type: .system)
        button1.configuration = config
        let button2 = UIButton(type: .system)
        config.title = "More"
        button2.configuration = config
        let stackView = UIStackView(arrangedSubviews: [button1, button2])
        stackView.axis = .horizontal
        stackView.spacing = 16
        stackView.translatesAutoresizingMaskIntoConstraints = false
        // iOS26新增
        let interaction = UIScrollEdgeElementContainerInteraction()
        interaction.scrollView = scrollView
        interaction.edge = .bottom
        // 关联视图
        stackView.addInteraction(interaction)
        return stackView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(scrollView)
        view.addSubview(stackView)

        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            contentView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
            contentView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
            contentView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
            contentView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
            contentView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
            contentView.heightAnchor.constraint(equalToConstant: 1000),
            stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            stackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
        ])
    }
}

效果

相关推荐
码云之上4 小时前
小程序 iOS 多输入框焦点乱序脱坑记
ios·微信小程序·taro
2501_9159090612 小时前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
随遇丿而安14 小时前
第 1 周:别小看 `UILabel`,它跟 Android TextView 不只是名字不同
ios
游戏开发爱好者82 天前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
Anhty3 天前
2026 实测 4 款 AI 变声器|QQ 聊天伪装声线,告别僵硬假声
人工智能·功能测试·ios·智能手机·安卓
唔663 天前
flutter web iOS 在浏览器加载中文慢的问题
前端·flutter·ios
ZacJi3 天前
我为 DeepSeek Harness 写了一个 dsh-apple-mode 插件
ios·deepseek·vibecoding
2501_915921433 天前
抓包鹰 Traceeagle 解除证书绑定,SSL Pinning 解除
网络·网络协议·网络安全·ios·adb·https·ssl
大熊猫侯佩3 天前
Xcode 预览新升级:arguments 让界面调试井井有条
swiftui·xcode·swift
人月神话Lee4 天前
我做了个不要账号、不要定位权限的旅行 App,聊聊那些「不做」的决定
ios·ai编程·产品