iOS26适配指南之UIViewController

介绍

WWDC24 中 UIViewController 增加了 5 种特殊的转场效果zoomcoverVerticalflipHorizontalcrossDissolvepartialCurl。但是触发条件仅限于 UIView,WWDC25 将zoom的触发条件扩展至 UIBarButtonItem。

使用

  • 代码。
swift 复制代码
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let barButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(barButtonItemClicked))
        navigationItem.rightBarButtonItem = barButtonItem
    }

    @objc func barButtonItemClicked(_ sender: Any) {
        let nextViewController = NextViewController()
        nextViewController.view.backgroundColor = .systemRed
        nextViewController.preferredTransition = .zoom { context in
            guard context.zoomedViewController is NextViewController else {
                fatalError("Unable to access the current UIViewController.")
            }
            // iOS26新增,返回触发的UIBarButtonItem
            return self.navigationItem.rightBarButtonItem
        }
        present(nextViewController, animated: true)
    }
}

class NextViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemGreen
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        dismiss(animated: true)
    }
}
  • 效果。
相关推荐
HarderCoder3 小时前
iOS 知识积累第一弹:从 struct 到 APP 生命周期的全景复盘
ios
叽哥13 小时前
Flutter Riverpod上手指南
android·flutter·ios
HarderCoder1 天前
Swift 6 并发时代,如何优雅地“抢救”你的单例?
swift
zhangmeng1 天前
FlutterBoost在iOS26真机运行崩溃问题
flutter·app·swift
HarderCoder1 天前
SwiftUI 踩坑记:onAppear / task 不回调?90% 撞上了“空壳视图”!
swift
HarderCoder1 天前
@isolated(any) 深度解析:Swift 并发中的“隔离追踪器”
swift
大熊猫侯佩2 天前
桃花岛 Xcode 构建秘籍:Swift 中的 “Feature Flags” 心法
app·xcode·swift
用户092 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan2 天前
iOS26适配指南之UIColor
ios·swift
HarderCoder2 天前
Swift 6.2 新特性 `@concurrent` 完全导读
swift