iOS26适配指南之@Observable Object

介绍

  • UIKit 支持@Observable类型。
  • 修饰的类型必须是类而不能是结构体。
  • 当其中的数据(属性值)发生更改时,相应的 UI 能够自动更新,而无需手动调用setNeedsLayout()setNeedsDisplay()layoutIfNeeded()等方法。
  • 需要将 UI 更新的代码放在 UIView 的layoutSubviews()或者 UIViewController 的viewWillLayoutSubviews()方法中。当@Observable中的数据发生变化时,layoutSubviews()viewWillLayoutSubviews()方法会自动调用。
  • 该功能可以支持到 iOS 18,但需要在 Info.plist 文件中增加字段UIObservationTrackingEnabled,并且将其值设置为YES

案例

swift 复制代码
import UIKit

// MARK: - PhoneModel
@Observable class PhoneModel {
    var name: String
    var osName: String

    init(name: String, osName: String) {
        self.name = name
        self.osName = osName
    }

    static func getPhone() -> [PhoneModel] {
        let phoneNames = ["iPhone 16", "iPhone 16 Plus", "iPhone 16 Pro", "iPhone 16 Pro Max"]
        let osNames = ["iOS 18", "iOS 18", "iOS 18", "iOS 18"]
        var phoneModels = [PhoneModel]()
        for i in 0 ..< phoneNames.count {
            let phoneModel = PhoneModel(name: phoneNames[i], osName: osNames[I])
            phoneModels.append(phoneModel)
        }
        return phoneModels
    }
}

// MARK: - UITableViewCell
class CustomTableViewCell: UITableViewCell {
    lazy var phoneLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: UIScreen.main.bounds.midX - 180, y: 200, width: 360, height: 100))
        label.textColor = .white
        label.font = .systemFont(ofSize: 40, weight: .bold)
        label.textAlignment = .center
        return label
    }()

    lazy var osLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: UIScreen.main.bounds.midX - 75, y: 400, width: 150, height: 50))
        label.textColor = .white
        label.font = .systemFont(ofSize: 25)
        label.textAlignment = .center
        return label
    }()

    var phoneModel: PhoneModel?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(phoneLabel)
        contentView.addSubview(osLabel)
        contentView.backgroundColor = .black
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        // UI更新
        phoneLabel.text = phoneModel?.name
        osLabel.text = phoneModel?.osName
    }
}

// MARK: - UIViewController
class ViewController: UIViewController {
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
        tableView.dataSource = self
        tableView.rowHeight = UIScreen.main.bounds.height
        tableView.isPagingEnabled = true
        tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "custom")
        return tableView
    }()

    let phoneModels = PhoneModel.getPhone()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            let phoneModel = self.phoneModels[0]
            phoneModel.name = "iPhone 17"
            phoneModel.osName = "iOS 26"
        }
    }
}

// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return phoneModels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "custom", for: indexPath) as! CustomTableViewCell
        cell.phoneModel = phoneModels[indexPath.row]
        return cell
    }
}

效果

相关推荐
电话交换机IPPBX-3CX1 天前
电话交换机软件 3CX iOS 应用 V5.4 Beta 更新
ios·软件更新·ip pbx·电话交换机
YGGP1 天前
【Swift】LeetCode 73. 矩阵置零
swift
初遇你时动了情1 天前
uniapp/flutter中实现苹果IOS 26 毛玻璃效果、跟随滑动放大动画
flutter·ios·uni-app
2501_916007471 天前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
CV大师杨某1 天前
如何在uni-app中禁用iOS橡皮筋效果?
ios·uni-app
2501_915918412 天前
iOS 上架应用市场全流程指南,App Store 审核机制、证书管理与跨平台免 Mac 上传发布方案(含开心上架实战)
android·macos·ios·小程序·uni-app·cocoa·iphone
C_philadd2 天前
Xcode26升级以后重要
ios
2501_915909062 天前
HTTPS 错误排查实战,从握手到应用层的工程化流程
网络协议·http·ios·小程序·https·uni-app·iphone
美狐美颜sdk2 天前
跨平台直播美颜sdk集成攻略:Android、iOS与Web的统一方案
android·前端·ios
2501_915106322 天前
“HTTPS Everywhere” 的工程化实践,从全面加密到排查与真机取证
网络协议·http·ios·小程序·https·uni-app·iphone