iOS 17新特性以及适配细节汇总
1、UIScrollView
增加了属性
增加了属性
allowsKeyboardScrolling
表示是否根据连接的物理键盘的方向键而滚动。
import UIKit class ViewController: UIViewController { lazy var scrollView: UIScrollView = { let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width)) let imageView = UIImageView(image: UIImage(named: "img")) scrollView.addSubview(imageView) scrollView.contentSize = imageView.bounds.size // iOS17新增,默认为true scrollView.isScrollEnabled = false return scrollView }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(scrollView) } }
2、applicationIconBadgeNumber
UIApplication 的applicationIconBadgeNumber
属性被废弃,建议使用UNUserNotificationCenter.current().setBadgeCount()
方法。
import UIKit import UserNotifications class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { // iOS17之后设置角标,需要先授权 // UNUserNotificationCenter.current().setBadgeCount(10) UNUserNotificationCenter.current().setBadgeCount(10) { error in if let error { print(error) } } } }
3、UIDocumentViewController
新增视图控制器,用于显示与管理本地或者云端文档。
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let documentViewController = UIDocumentViewController() documentViewController.openDocument { _ in print("打开文档") } present(documentViewController, animated: true) } }
4、UIHoverStyle
UIView 增加了一个hoverStyle
属性,可以设置鼠标移动到 UIView 之上的效果。
import UIKit class ViewController: UIViewController { lazy var redView: UIView = { let view = UIView(frame: CGRect(x: 200, y: 200, width: 200, height: 200)) view.backgroundColor = .red // iOS17新增UIHoverStyle,可以设置Hover的效果与形状(UIShape) let hoverStyle = UIHoverStyle(effect: .lift, shape: .capsule) // iOS17新增,鼠标移动到UIView之上的效果 view.hoverStyle = hoverStyle return view }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(redView) } }
5、编译报错cfstring constant not pointer aligned
解决办法:Build Settings -> Other Linker Flags 加入-ld64
6、编译报错Sandbox:rsync.sanba deny(1) file-write-create xxx
使用 Xcode15 新建项目后,pod 引入部分第三方会报上面的错误
解决办法:Build Settings 搜索 sandbox,把 Build Options 中的 User Script Sandboxing改为 NO
7、编译报错UIGraphicsBeginImageContextWithOptions崩溃
参考链接:UIGraphicsBeginImageContext Deprecated
YYText使用时会崩溃在UIGraphicsBeginImageContextWithOptions
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 本地部署 DeepSeek:小白也能轻松搞定!
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)