扬庆の博客

iOS15适配新增特性

字数统计: 592阅读时长: 2 min
2021/12/03 Share

iOS15新增UIKit要点特性

  • UINavigationBar, UIToolBar, UITabBar 设置颜色, 需要使用UIBarAppearance API

scrollEdgeAppearance

standardAppearance

上面这两个要设置成一个appearance, 不然导航栏带滚动视图的时候,

可能会导致本来设置的颜色变成透明的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// UINavigationBar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backgroundColor = .red
...
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
// UIToolbar
let toolBarAppearance = UIToolbarAppearance()
toolBarAppearance.backgroundColor = .blue
...
navigationController?.toolbar.scrollEdgeAppearance = toolBarAppearance
navigationController?.toolbar.standardAppearance = toolBarAppearance
// UITabBar
let tabBarAppearance = UITabBarAppearance()
toolBarAppearance.backgroundColor = .purple
...
tabBarController?.tabBar.scrollEdgeAppearance = tabBarAppearance
tabBarController?.tabBar.standardAppearance = tabBarAppearance

iOS15导航栏


  • UITableView 新增了属性 sectionHeaderTopPadding
1
tableView.sectionHeaderTopPadding = 0 

  • UIButton支持更多配置

UIButton.configuration 是一个新的结构体, 它指定按钮机器内容的外观和行为

如:cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。

iOS15新增button

如何使用UIButton.Configure ?

1
2
3
4
5
6
7
8
9
10
11
// 创建一个config
var config = UIButton.Configuration.tinted()
// 设置按钮标题
config.title = "Add to Cart"
// 设置按钮图片
config.image = UIImage(systemName: "cart.badge.plus")
// 设置图片居于按钮 左侧/右侧
config.imagePlacement = .trailing
// 将config 应用到按钮上
addToCartButton = UIButton(configuration: config, primaryAction: ...)

利用.configurationUpdateHandler 来控制按钮图片和子标题文字的显示 .

1
2
3
4
5
6
7
8
addToCartButton.configurationUpdateHandler = { [unowned self] button in
var config = button.configuration
config?.image = button.isHighlighted
? UIImage(systemName: "cart.fill.badge.plus")
: UIImage(systemName: "cart.badge.plus")
config?.subTitle = self.itemQuantityDescription
button.configuration = config
}
1
2
3
4
5
private var itemQuantityDescription: String? {
didSet {
addToCartButton.setNeedsUpdateConfiguration() // 更新按钮显示文字
}
}

iOS15中UIKit提供了四种基本样式

gray \ filled \ tinted \ pulldown

样例:

1
2
3
4
5
6
7
8
// iOS15特性一个完整的自定义按钮
var config = UIButton.Configuration.filled()
config.buttonSize = .large
config.image = UIImage(systemName: "cart.fill")
config.title = "Checkout"
config.background.backgroudColor = .buttonEmporium

let checkButton = UIButton(configuration: config, primaryAction: ...)

  • CLLocationButton 用于一次性定位授权, 该内容在CoreLocationUI模块

如果需要获取定位的详细信息依然需要借助于CoreLocation

1
2
3
4
5
6
7
8
let locationButton = CLLocationButton()
locationButton.label = .currentLocation
locationButton.fontSize = 20
locationButton.icon = .arrowFilled
locationButton.cornerRadius = 10
locationButton.tintColor = UIColor.systemPink
locationButton.backgroudColor = UIColor.systemGreen
locationButton.addTarget(self, action: #selector(), for: .touchUpInside)

CLLocationButton


  • UIImage 调整尺寸的方法
1
2
3
4
5
UIImage(named: "sv.png")?.preparingThumbnail(of: CGSize(width: 200, height: 100))
UIImage(named: "sv.png")?.prepareThumbnail(of: CGSize(width: 200, height: 100)) { image in
// update UI in main thread
}
await UIImage(named:"sv.png")?.byPreparingThumbnail(ofSize: CGSize(width: 100, height: 200))

新增UISheetPresentationController

管理工作表的外观和行为的表示控制器。

UISheetPresentationController官方文档

WWDC Video

  • 用法
  1. 新建一个要 present 的 viewcontroller
  2. xxxvc.sheetPresentationController 来设置sheet的相关属性.

例如: detents (.medium() , .large() 范围 全屏和半屏之间滚动 )

largestUndimmedDetentIdentifier 设置为medium 弹出时底部视图不会变暗

还有:

prefersScrollingExpandsWhenScrolledToEdge

prefersEdgeAttachedInCompactHeight

widthFollowsPreferredContentSizeWhenEdgeAttached

1
present(viewControllerToPresent, animated: true, completion: nil)

sheet

CATALOG
  1. 1. iOS15新增UIKit要点特性
    1. 1.1. 新增UISheetPresentationController