iOS27适配指南之iPhone Duo(一)

39 阅读1分钟

介绍

iOS 27.1 新增了一组专为 iPhone Duo 折叠屏设计的布局 API:UIKit 的 UIArrangementViewController 和 SwiftUI 的 ArrangementView。两者都用来管理「主、次」两块内容的分栏与叠放,控制折叠屏左右(或上下)两部分的内容布局。

UIKit

UIArrangementViewController 继承 UIArrangementViewController,用 setViewController(_:for:) 指定主、次内容,再用 updateArrangement(_:) 设置布局规则。

import UIKit

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

        // 管理两个子控制器
        setViewController(PlayerViewController(), for: .primary)
        setViewController(QueueViewController(), for: .secondary)
        // 设置布局规则
        updateArrangement(.split)
    }
}

要点:

  • .primary 与 .secondary 对应主、次两块内容。
  • updateArrangement(.split) 指定分栏布局。
  • 两个子控制器是普通的 UIViewController,无需感知折叠状态。

示例:播放器 + 播放队列

主内容是播放控制页:

class PlayerViewController: UIViewController {
    lazy var button: UIButton = {
        let button = UIButton(type: .system)
        button.configuration = .filled()
        button.configuration?.title = "播放"
        button.addAction(UIAction { [weak self, weak button] _ in
            guard let self else { return }
            self.isPlaying.toggle()
            button?.configuration?.title = self.isPlaying ? "暂停" : "播放"
        }, for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    var isPlaying = false

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemGray
        view.addSubview(button)
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor),
            button.leadingAnchor.constraint(greaterThanOrEqualTo: view.layoutMarginsGuide.leadingAnchor),
            button.trailingAnchor.constraint(lessThanOrEqualTo: view.layoutMarginsGuide.trailingAnchor)
        ])
    }
}

次内容是歌曲队列:

class QueueViewController: UIViewController {
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: view.frame)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.dataSource = self
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
    }
}

extension QueueViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        30
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        var content = cell.defaultContentConfiguration()
        content.text = "歌曲 \(indexPath.row + 1)"
        content.image = UIImage(systemName: "music.note")
        cell.contentConfiguration = content
        return cell
    }
}

SwiftUI

ArrangementView 接收两个闭包,第一个是主内容,secondary: 是次内容。显示风格通过 arrangementViewStyle 修饰符设置。

import SwiftUI

struct ContentView: View {
    @State private var isPlaying = false

    var body: some View {
        NavigationStack {
            ArrangementView {
                VStack {
                    Spacer()

                    Button {
                        isPlaying.toggle()
                    } label: {
                        Label(isPlaying ? "暂停" : "播放", systemImage: isPlaying ? "pause.fill" : "play.fill")
                    }
                    .buttonStyle(.borderedProminent)
                    .padding()
                }

            } secondary: {
                ZStack {
                    LinearGradient(
                        colors: [.indigo, .blue],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )

                    Image(systemName: "music.note")
                        .font(.system(size: 80))
                        .foregroundStyle(.white)
                }
                .padding()
            }
            .arrangementViewStyle(.overlay)
            .navigationTitle("堆叠式排列")
        }
    }
}

要点:

  • .split:无论是否展开,主、次内容始终分开显示。
  • .overlay:未展开时分离显示,展开后次内容叠加在主内容之上。

效果

iPhone Duo.gif